-
Notifications
You must be signed in to change notification settings - Fork 344
/
Copy pathTwo_Sum.cpp
46 lines (35 loc) · 911 Bytes
/
Two_Sum.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
/*
Given an array of integers and an integer target,
return indices(zero indexed) of the two numbers if exists such that they sum adds up to target.
*/
#include<bits/stdc++.h>
using namespace std;
vector<int> Two_Sum(vector<int> &arr, int target) {
std::unordered_map<int,int> mp;
for(int i=0;i<arr.size();i++){
if(mp.find(target-arr[i])!=mp.end())
return {mp[target-arr[i]],i};
mp[arr[i]]=i;
}
return {-1,-1};
}
int main()
{
cout << "Enter the size of the array: ";
int n;
cin >> n;
cout << "Enter the array: " << '\n';
vector<int> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << "Enter the target: ";
int target;
cin >> target;
vector<int> p = Two_Sum(arr, target);
if (p[0] == -1 && p[1] == -1)
cout << "Two numbers with sum "<<target<< " are not present in the array" << endl;
else
cout << "Indices are " << p[0] << " and " << p[1] << endl;
return 0;
}