Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0f6c82c

Browse files
+ All pairs whose sum is x (unsorted arrays)
1 parent e32ba87 commit 0f6c82c

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
#http://www.geeksforgeeks.org/given-two-unsorted-arrays-find-pairs-whose-sum-x/
2+
#Hashing
3+
4+
def sum_pairs(array1: list, array2: list, req_sum: int):
5+
sum_lists = []
6+
7+
#hash array1 for later searching
8+
hash1 = { x:x for x in array1 }
9+
10+
for y in array2:
11+
try:
12+
#if hash1[req_sum-y] exists
13+
x = hash1[req_sum - y]
14+
sum_lists.append([x,y])
15+
except:
16+
#the other number for pairing with y not found
17+
continue
18+
19+
return sum_lists
20+
21+
#main
22+
if __name__=="__main__":
23+
array1 = list(map(int,input().split()))
24+
array2 = list(map(int,input().split()))
25+
req_sum=int(input())
26+
27+
pairs = sum_pairs(array1,array2,req_sum)
28+
print("Sum pairs for", req_sum,"are")
29+
print([pair for pair in pairs])
30+
31+
32+
33+
"""
34+
Input Explanation :
35+
- List 1
36+
- List 2
37+
- Required sum
38+
39+
Input :
40+
-1 -2 4 -6 5 7
41+
6 3 4 0
42+
8
43+
44+
Output :
45+
Sum pairs for 8 are
46+
[[5, 3], [4, 4]]
47+
48+
"""
49+
50+
'''
51+
Given two unsorted arrays of distinct elements, the task is to find all pairs from both arrays whose sum is equal to x.
52+
53+
Examples:
54+
55+
Input : arr1[] = {-1, -2, 4, -6, 5, 7}
56+
arr2[] = {6, 3, 4, 0}
57+
x = 8
58+
Output : 4 4
59+
5 3
60+
61+
Input : arr1[] = {1, 2, 4, 5, 7}
62+
arr2[] = {5, 6, 3, 4, 8}
63+
x = 9
64+
Output : 1 8
65+
4 5
66+
5 4
67+
Asked in : Amazon
68+
69+
Recommended: Please solve it on "PRACTICE " first, before moving on to the solution.
70+
A Naive approach is to simply run two loops and pick elements from both arrays. One by one check that both elements sum is equal to given value x or not.
71+
72+
// C++ program to find all pairs in both arrays
73+
// whose sum is equal to given value x
74+
#include<bits/stdc++.h>
75+
using namespace std;
76+
77+
// Function to print all pairs in both arrays
78+
// whose sum is equal to given value x
79+
void findPairs(int arr1[], int arr2[], int n,
80+
int m, int x)
81+
{
82+
for (int i=0; i<n; i++)
83+
for (int j=0; j<m; j++)
84+
if (arr1[i] + arr2[j] == x)
85+
cout << arr1[i] << " "
86+
<< arr2[j] << endl;
87+
}
88+
89+
// Driver code
90+
int main()
91+
{
92+
int arr1[] = {1, 2, 3, 7, 5, 4};
93+
int arr2[] = {0, 7, 4, 3, 2, 1};
94+
int n = sizeof(arr1)/sizeof(int);
95+
int m = sizeof(arr2)/sizeof(int);
96+
int x = 8;
97+
findPairs(arr1, arr2, n, m, x);
98+
return 0;
99+
}
100+
Run on IDE
101+
Output:
102+
103+
1 7
104+
7 1
105+
5 3
106+
4 4
107+
Time Complexity : O(n^2)
108+
Auxiliary Space : O(1)
109+
110+
An Efficient solution of this problem is to hashing. Hash table is implemented using unordered_set in C++. We store all first array elements in hash table. For elements of second array, we subtract every element from x and check the result in hash table. If result is present, we print the element and key in hash (which is an element of first array).
111+
C++Java
112+
// C++ program to find all pair in both arrays
113+
// whose sum is equal to given value x
114+
#include<bits/stdc++.h>
115+
using namespace std;
116+
117+
// Function to find all pairs in both arrays
118+
// whose sum is equal to given value x
119+
void findPairs(int arr1[], int arr2[], int n,
120+
int m, int x)
121+
{
122+
// Insert all elements of first array in a hash
123+
unordered_set<int> s;
124+
for (int i=0; i<n; i++)
125+
s.insert(arr1[i]);
126+
127+
// Subtract sum from second array elements one
128+
// by one and check it's present in array first
129+
// or not
130+
for (int j=0; j<m; j++)
131+
if (s.find(x - arr2[j]) != s.end())
132+
cout << x-arr2[j] << " "
133+
<< arr2[j] << endl;
134+
}
135+
136+
// Driver code
137+
int main()
138+
{
139+
int arr1[] = {1, 0, -4, 7, 6, 4};
140+
int arr2[] = {0 ,2, 4, -3, 2, 1};
141+
int x = 8;
142+
int n = sizeof(arr1)/sizeof(int);
143+
int m = sizeof(arr2)/sizeof(int);
144+
findPairs(arr1, arr2, n, m, x);
145+
return 0;
146+
}
147+
Run on IDE
148+
149+
Output:
150+
6 2
151+
4 4
152+
6 2
153+
7 1
154+
Time Complexity : O(n)
155+
Auxiliary Space : O(n)
156+
'''

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /