-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.cpp
More file actions
38 lines (38 loc) · 1.02 KB
/
TwoSum.cpp
File metadata and controls
38 lines (38 loc) · 1.02 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
#include <map>
string read(int n, vector<int> book, int target)
{
// Write your code here.
// int ptr1 = book.begin();
// int ptr2 = book.end();
// for(int i=0;i<n;i++){
// for(int j=i;j<n;j++){
// if(book[i]+book[j]==target) return "YES";
// }
// }
// return "NO"; - 0(n^2)
// approach 2 - using map
// map<int,int> mpp;
// for(int i=0;i<n;i++){
// int a = book[i];
// int left = target - a;
// if(mpp.find(left)!=mpp.end()){
// return "YES";
// }
// mpp[a]= i;
// }
// return "NO"; // 0-(n),0(n)
// approach 3 - using sorting and 2 ptrs(basically with which I approached the problem first with -noice)
sort(book.begin(),book.end());
int left = 0;
int right = n-1;
while(left<=right){
if(book[left]+book[right]==target) return "YES";
if(book[left]+book[right]>target){
right--;
}
else{
left++;
}
}
return "NO"; // 0(n),0(1)
}