forked from srinidh-007/Coding_Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwoSum.cpp
More file actions
51 lines (41 loc) · 1.18 KB
/
twoSum.cpp
File metadata and controls
51 lines (41 loc) · 1.18 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
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
/*
Two Sum will take a n array of numbers and a target as input
and returns the pair of indicies of array which add up to that target
if such pair doesn't exist, it will simply return an empty array
*/
vector<int> twoSum(vector<int>& nums, int target) {
// a map to store value and it's corresponding index
unordered_map<int, int> mp;
// a vector to store final result
vector<int> x;
// iterating through the array
for (int i = 0; i < nums.size(); i++) {
// check if map contains the target - curr_value
// if yes we found the pair
// else add it to map for referencing further in the array
if (mp.find(target-nums[i]) != mp.end()) {
x.push_back(i);
x.push_back(mp[target-nums[i]]);
break;
} else {
mp[nums[i]] = i;
}
}
return x;
}
/*
Driver code
*/
int main() {
vector<int> arr;
arr.push_back(1);
arr.push_back(4);
arr.push_back(6);
arr.push_back(3);
arr.push_back(8);
vector<int> res = twoSum(arr, 10);
cout<<arr[res[0]]<<" "<<arr[res[1]]<<endl;
}