-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTwoSum.cpp
77 lines (68 loc) · 1.55 KB
/
TwoSum.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
#include <stdexcept>
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
class TwoSum
{
public:
static pair<int, int> findTwoSum(vector<int>& list, int sum)
{
pair<int,int> indices;
indices.first=-1;
indices.second=-1;
int i,j;
i=j=0;
int diff;
int index;
for(i=0;i<list.size()-1;i++){
diff=sum-list[i];
index=binarySearch(list,diff);
//cout << diff << index ;
if(index==-1)
{
continue;
}
else if(index!=i)
{
indices.first=i;
indices.second=index;
return indices;
}
}
return indices;
}
static int binarySearch(vector<int>& list, int val)
{
int first , last, mid;
first=0;
last=list.size();
while(first<=last){
mid=(first+last)/2;
if(list[mid]==val){
return mid;
}
else if(list[mid]>val){
last=mid-1;
}
else
{
first =mid+1;
}
}
return -1;
}
};
#ifndef RunTests
int main()
{
vector<int> list;
list.push_back(1);
list.push_back(3);
list.push_back(3);
list.push_back(7);
list.push_back(8);
pair<int, int> indices = TwoSum::findTwoSum(list, 11);
cout << indices.first << '\n' << indices.second;
}
#endif