-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1_Two-sum.cpp
More file actions
105 lines (99 loc) Β· 2.78 KB
/
1_Two-sum.cpp
File metadata and controls
105 lines (99 loc) Β· 2.78 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
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, and you may not use the same element twice.
You can return the answer in any order.
*/
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
// Brute Force O(n^2)
vector<int> twoSum(vector<int>& nums, int target) {
for(int i=0;i<nums.size();i++){
for(int j=i+1;j<nums.size();j++){
if(nums[i]+nums[j]==target){
return {i,j};
}
}
}
return {};
}
// Login_approach O(nlog(n));
vector<pair<int,int>> twoSumPair(vector<int>& arr, int target, int n)
{
// Write your code here.
vector<pair<int,int>> output ;
sort(arr.begin(),arr.end());
int i =0;
int j = n - 1;
while (i<j) {
pair<int,int> p;
int sum = arr[i] + arr[j];
if (sum == target) {
p.first = arr[i];
p.second = arr[j];
output.push_back(p);
i++;
j--;
} else if (sum < target) {
i++;
} else {
j--;
}
}
if(!output.size()){
output.push_back({-1,-1});
}
return output;
}
// using the Hash map
vector<int> twoSumHashMap(vector<int>& nums, int target) {
unordered_map<int, int> mp;
for(int i = 0; i < nums.size(); i++){
if(mp.find(target - nums[i]) == mp.end())
mp[nums[i]] = i;
else
return {mp[target - nums[i]], i};
}
return {-1, -1};
}
// Most Optimized
vector<int> twoSumOptimized(vector<int>& nums, int target) {
int n = nums.size();
vector<pair<int, int>> numsWithIndices;
for(int i=0; i<n; i++)
numsWithIndices.push_back({nums[i], i});
sort(numsWithIndices.begin(), numsWithIndices.end());
int left = 0, right = n-1;
while (left < right)
{
int currentSum = numsWithIndices[left].first + numsWithIndices[right].first;
if(currentSum == target)
return {numsWithIndices[left].second, numsWithIndices[right].second};
else if(currentSum < target)
left++;
else
right--;
}
return {-1, -1};
}
};
int main(){
Solution s;
int n;
cin>>n;
vector<int> arr(n);
for(int i=0;i<n;i++){
cin>>arr[i];
}
int target;
cin>>target;
for(auto x:s.twoSum(arr,target)){
cout<<x<<" ";
}
vector<pair<int,int>> res = s.twoSumPair(arr,target,n);
for(auto p:res){
cout<<p.first<<":"<<p.second<<endl;
}
return 0;
}