forked from codemistic/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindSum.cpp
58 lines (47 loc) · 943 Bytes
/
findSum.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
#include <bits/stdc++.h>
using namespace std;
class Solution{
public:
vector<pair<int,int>> allPairs(int A[], int B[], int N, int M, int X)
{
vector<pair<int,int>>v;
for(int i =0;i<N;i++){
for(int j =0;j<M;j++){
if(A[i]+B[j]==X){
v.push_back({A[i],B[j]});
}
}
}
sort(v.begin(),v.end());
return v;
}
};
int main()
{
int 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;
}