-
Notifications
You must be signed in to change notification settings - Fork 0
/
Find all pairs with a given sum.cpp
85 lines (77 loc) · 1.99 KB
/
Find all pairs with a given sum.cpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ft first
#define sc second
#define pb push_back
#define ins insert
#define all(v) v.begin(), v.end()
#define Sort(v) sort(all(v))
#define s(a, n) sort(a, a + n)
#define f1(i,a,b) for(ll i=a;i<b;i++)
#define f2(i,a,b,c) for(ll i=a;i<b;i+=c)
#define f3(i,a,b) for(ll i=a;i>=b;i--)
#define f4(i,a,b,c) for(ll i=a;i>=b;i-=c)
#define jatin(t) int t; cin >> t; while(t--)
const int N = 1e5+2, MOD = 1e9+7;
//int array[N];
//cout << (condition ? "YES\n" : "NO\n");
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution{
public:
vector<pair<int,int>> allPairs(int A[], int B[], int N, int M, int X)
{
// Your code goes here
vector<pair<int,int>> v;
set<int,greater<int> > s;
map<int,int> mp;
for(int i=0;i<M;i++)
{
s.insert(B[i]);
}
for(int i=0;i<N;i++){
mp[A[i]]=1;
}
set<int,greater<int> >:: iterator it;
for(it=s.begin();it!=s.end();it++){
int a=X-(*it);
if(mp[a]==1){
v.push_back(make_pair(a,*it));
}
}
return v;
}
};
// { Driver Code Starts.
int main() {
long long t;
cin >> t;
while(t--){
int N, M, X;
cin >> N >> M >> X;
int A[N], B[M];
for(int i = 0;i<N;i++)
cin >> A[i];
for(int i = 0;i<M;i++)
cin >> B[i];
Solution ob;
vector<pair<int,int>> vp = ob.allPairs(A, B, N, M, X);
int sz = vp.size();
if(sz==0)
cout<<-1<<endl;
else{
for(int i=0;i<sz;i++){
if(i==0)
cout<<vp[i].first<<" "<<vp[i].second;
else
cout<<", "<<vp[i].first<<" "<<vp[i].second;
}
cout<<endl;
}
}
return 0;
} // } Driver Code Ends