-
Notifications
You must be signed in to change notification settings - Fork 0
/
4Sum.cpp
52 lines (48 loc) · 1.25 KB
/
4Sum.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
// Brute Force TC O(N^4) SC O(1)
#include<bits/stdc++.h>
string fourSum(vector<int> arr, int target, int n) {
for(int i=0;i<n-3;++i)
{
for(int j=i+1;j<n-2;++j)
{
for(int k=j+1;k<n-1;++k)
{
for(int l=k+1;l<n;++l)
{
if(arr[i]+arr[j]+arr[k]+arr[l]==target)
return "Yes";
}
}
}
}
return "No";
}
// Optimized using hashmap TC O(N^2) SC O(N^2)
#include<bits/stdc++.h>
bool commonIdx(pair<int,int> p1, pair<int,int> p2){
return p1.first==p2.first || p1.first==p2.second || p1.second==p2.first || p1.second==p2.second;
}
string fourSum(vector<int> arr, int target, int n) {
unordered_map<int,pair<int,int>> mp;
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
mp[arr[i]+arr[j]]={
i,
j
};
}
}
for(int i=0;i<n-1;i++){
for(int j=0;j<n;j++){
int requiredSum = target-(arr[i]+arr[j]);
if(mp.find(requiredSum)!=mp.end() && !commonIdx(mp[requiredSum],{
i,
j
}))
{
return "Yes";
}
}
}
return "No";
}