-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpairOfSum.cpp
More file actions
42 lines (29 loc) · 831 Bytes
/
pairOfSum.cpp
File metadata and controls
42 lines (29 loc) · 831 Bytes
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
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> pairSum(vector<int> &arr, int s){
int i = 0, j = arr.size() - 1;
vector<vector<int>> result;
sort(arr.begin(), arr.end());
while (i < j) {
if ((arr[i] + arr[j]) == s) {
vector<int> temp;
temp.push_back(arr[i++]);
temp.push_back(arr[j--]);
result.push_back(temp);
}
else if ((arr[i] + arr[j]) > s)
j--;
else
i++;
}
return result;
}
int main () {
vector<int> vec = {2,-3,3,3,-2};
vector<vector<int>> result = pairSum(vec, 5);
std::vector< std::vector<int> >::const_iterator row;
std::vector<int>::const_iterator col;
for (row = result.begin(); row != result.end(); row++)
for (col = row->begin(); col != row->end(); ++col)
cout << *col << " ";
}