-
Notifications
You must be signed in to change notification settings - Fork 255
/
2Sum.cpp
57 lines (40 loc) · 1.31 KB
/
2Sum.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
/*
-> Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
-> You may assume that each input would have exactly one solution*/
/*
-> we will solve this question using two pointer , one pointer will be pointing towards the start and another pointer will pointing towards the
the last elemnt at every iteration we will be checking that sum of our element at pointers is equal to the target or not*/
#include<bits/stdc++.h>
using namespace std;
void twoSum(vector<int>& nums, int target) {
int low=0;
int high=nums.size()-1;
vector<int>ans;
vector<pair<int,int>>pr;
for(int i =0;i<nums.size();i++)
{
pr.push_back({nums[i],i});
}
sort(pr.begin(),pr.end());
while(low<high)
{
if(pr[low].first+pr[high].first == target)
{
ans.push_back(pr[low].second);
ans.push_back(pr[high].second);
break;
}
else if(pr[low].first+pr[high].first < target) low++;
else high--;
}
for(auto it:ans) cout<<it<<" ";
}
int main()
{
vector<int>v;
for(int i=0;i<5;i++) v.push_back(i+1);
int target = 9;
twoSum(v,target);
return 0;
}
//code for it