-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday16_3.java
51 lines (39 loc) · 1.21 KB
/
day16_3.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Given two unsorted arrays A of size N and B of size M of distinct elements, the task is to find all pairs from both arrays whose sum is equal to X.
// Note: All pairs should be printed in increasing order of u. For eg. for two pairs (u1,v1) and (u2,v2), if u1 < u2 then
// (u1,v1) should be printed first else second.
//User function Template for Java
/*
class pair {
long first, second;
public pair(long first, long second)
{
this.first = first;
this.second = second;
}
}
*/
class Solution {
public pair[] allPairs( long A[], long B[], long N, long M, long X) {
// Your code goes here
Arrays.sort(A);
Arrays.sort(B);
ArrayList<pair> ans = new ArrayList<>();
ArrayList<Long> b = new ArrayList<>();
int m = B.length;
int n = A.length;
for(int i=0; i<m; i++)
{
b.add(B[i]);
}
for(int i=0; i<n ; i++)
{
if(b.contains(X-A[i]))
{
pair p = new pair(A[i], X-A[i]);
ans.add(p);
}
}
pair[] all = ans.toArray(new pair[ans.size()]);
return all;
}
}