-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathTwoSum.cpp
More file actions
55 lines (50 loc) · 1.36 KB
/
TwoSum.cpp
File metadata and controls
55 lines (50 loc) · 1.36 KB
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
/* This code is to check Two Sum */
#include<bits/stdc++.h>
using namespace std;
//two pointer solution
vector<int> twoSum(vector<int>& nums, int target) {
int front = 0, rear = nums.size() - 1;
sort(nums.begin(), nums.end());
while(front < rear) {
int sum = nums[front] + nums[rear];
if (sum == target)
break;
else if (sum > target)
rear--;
else
front++;
}
return {front, rear};
};
//brute force solution
vector<int> twoSum(vector<int>& nums, int target) {
int len = nums.size();
for(int i = 0; i < len; i++) {
for(int j = i + 1; j < len; j++) {
if(nums[j] == target - nums[i])
return { i, j };
}
}
return {-1, -1};
};
// hash map solution
vector<int> twoSum(vector<int>& nums, int target) {
map<int, int> map;
vector<int> pairs;
for(int i = 0; i < nums.size(); i++) {
int complement = target - nums[i];
if(map.find(complement) != map.end()) {
pairs.push_back(map.find(complement)->second);
pairs.push_back(i);
break;
}
map.insert(pair<int, int>(nums[i], i));
}
return pairs;
};
int main() {
vector<int>nums = {1,4,3,6,5,8};
vector<int>pair = twoSum(nums,4);
cout << "pair indices are : " << pair[0] << " " << pair[1] << endl;
return 0;
}