给定一个由N 个正整数组成的数组arr[] ,任务是打印给定数组中不存在总和的所有数组元素对。如果不存在这样的对,则打印“-1” 。
例子:
Input: arr[] = {2, 4, 2, 6}
Output:
(2, 6)
(4, 6)
(2, 6)
Explanation:
All possible pairs in the array are (2, 4), (2, 2), (2, 6), (4, 2), (4, 6) and (2, 6).
Among these, the pairs (2, 6), (4, 6) and (2, 6) have sums {8, 10, 8} respectively which are not present in the array.
Input: arr[] = {1, 1, 2, 3}
Output:
(2, 3)
Explanation:
All possible pairs in the array are (1, 1), (1, 2), (1, 3), (1, 2), (1, 3) and (2, 3).
Among these, the only pair whose sum is not present in the array is (2, 3).
天真的方法:
解决这个问题的最简单的方法是一个一个地生成所有可能的对,并通过遍历数组检查它的和是否存在于数组中。如果发现任何对的总和存在于数组中,则打印该对。否则,移动到下一对。
时间复杂度: O(N 3 )
辅助空间: O(N)
有效的方法:
这个问题可以使用 HashSet 来解决。请按照以下步骤解决问题:
- 将数组中的元素存储在HashSet 中。
- 现在,遍历所有数组元素并生成所有可能的对。
- 对于每一对,检查该对的总和是否存在于 HashSet 中。如果是,请打印该对。否则,移动到下一对。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print all pairs
// with sum not present in the array
void findPair(int arr[], int n)
{
int i, j;
// Corner Case
if (n < 2)
{
cout << "-1" << endl;
}
// Stores the distinct array
// elements
set hashMap;
for(int k = 0; k < n; k++)
{
hashMap.insert(arr[k]);
}
// Generate all possible pairs
for(i = 0; i < n - 1; i++)
{
for(j = i + 1; j < n; j++)
{
// Calculate sum of current pair
int sum = arr[i] + arr[j];
// Check if the sum exists in
// the HashSet or not
if (hashMap.find(sum) == hashMap.end())
{
cout << "(" << arr[i] << ", "
<< arr[j] << ")" << endl;
}
}
}
}
// Driver code
int main()
{
int arr[] = { 2, 4, 2, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
findPair(arr, n);
return 0;
}
// This code is contributed by divyeshrabadiya07
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG {
// Function to print all pairs
// with sum not present in the array
public static void findPair(
int[] arr, int n)
{
int i, j;
// Corner Case
if (n < 2) {
System.out.println("-1");
}
// Stores the distinct array
// elements
HashSet hashMap
= new HashSet();
for (Integer k : arr) {
hashMap.add(k);
}
// Generate all possible pairs
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
// Calculate sum of current pair
int sum = arr[i] + arr[j];
// Check if the sum exists in
// the HashSet or not
if (!hashMap.contains(sum)) {
System.out.println(
"(" + arr[i] + ", "
+ arr[j] + ")");
}
}
}
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 2, 4, 2, 6 };
int n = arr.length;
findPair(arr, n);
}
}
Python3
# Python3 program to implement
# the above approach
# Function to print all pairs
# with sum not present in the array
def findPair(arr, n):
# Corner Case
if (n < 2):
print("-1")
# Stores the distinct array
# elements
hashMap = []
for k in arr:
hashMap.append(k)
# Generate all possible pairs
for i in range (n - 1):
for j in range (i + 1, n):
# Calculate sum of current pair
sum = arr[i] + arr[j]
# Check if the sum exists in
# the HashSet or not
if sum not in hashMap:
print("(", arr[i] , ", ",
arr[j] , ")")
# Driver Code
if __name__ == "__main__":
arr = [ 2, 4, 2, 6 ]
n = len(arr)
findPair(arr, n)
# This code is contributed by ChitraNayal
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to print all pairs
// with sum not present in the array
public static void findPair(int[] arr, int n)
{
int i, j;
// Corner Case
if (n < 2)
{
Console.Write("-1");
}
// Stores the distinct array
// elements
HashSet hashMap = new HashSet();
foreach(int k in arr)
{
hashMap.Add(k);
}
// Generate all possible pairs
for(i = 0; i < n - 1; i++)
{
for(j = i + 1; j < n; j++)
{
// Calculate sum of current pair
int sum = arr[i] + arr[j];
// Check if the sum exists in
// the HashSet or not
if (!hashMap.Contains(sum))
{
Console.Write("(" + arr[i] +
", " + arr[j] +
")\n");
}
}
}
}
// Driver Code
public static void Main(string[] args)
{
int[] arr = { 2, 4, 2, 6 };
int n = arr.Length;
findPair(arr, n);
}
}
// This code is contributed by rutvik_56
Javascript
(2, 6)
(4, 6)
(2, 6)
时间复杂度: O(N 2 )
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。