-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.Pair sum
37 lines (33 loc) · 943 Bytes
/
2.Pair sum
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
#include <bits/stdc++.h>
#include <bits/stdc++.h>
vector<vector<int>> pairSum(vector<int> &arr, int s){
// Write your code here.
vector <vector <int> > ans ;
map <int,int> m ;
// sort(arr.begin(),arr.end());
for(int i = 0 ; i < arr.size();i++)
{
if(m.count(s - arr[i]) > 0 )
{
int k = m[s - arr[i]] ;
// cout << k << arr[i] << endl ;
for(int j = 0 ; j < k; j++) {
vector <int> res ;
if(arr[i] < s - arr[i])
{
res.push_back(arr[i]) ;
res.push_back(s - arr[i]) ;
}
else
{
res.push_back(s - arr[i]) ;
res.push_back(arr[i]) ;
}
ans.push_back(res) ;
}
}
m[arr[i]]++ ;
}
sort(ans.begin(), ans.end()) ;
return ans ;
}