-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFindSum.java
35 lines (28 loc) · 1.03 KB
/
FindSum.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package collectionz;
import java.util.*;
class FindSum {
// Function to find all pairs in both arrays
// whose sum is equal to given value x
public static void findPairs(int arr1[], int arr2[],
int n, int m, int x)
{
// Insert all elements of first array in a hash
HashMap<Integer, Integer> s = new HashMap<Integer, Integer>();
for (int i = 0; i < n; i++)
s.put(arr1[i], 0);
// Subtract sum from second array elements one
// by one and check it's present in array first
// or not
for (int j = 0; j < m; j++)
if (s.containsKey(x - arr2[j]))
System.out.println(x - arr2[j] + " " + arr2[j]);
}
/* Driver program to test above function */
public static void main(String[] args)
{
int arr1[] = { 1, 0, -4, 7, 6, 4 };
int arr2[] = { 0, 2, 4, -3, 2, 1 };
int x = 8;
findPairs(arr1, arr2, arr1.length, arr2.length, x);
}
}