给定一个由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
(2, 6)
(4, 6)
(2, 6)
时间复杂度: O(N 2 )
辅助空间: O(N)