-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFour-Sum.cpp
39 lines (34 loc) · 1.21 KB
/
Four-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
#include <bits/stdc++.h>
using namespace std;
string fourSum(vector<int> arr, int target, int n) {
// Sort the array
sort(arr.begin(), arr.end());
// Iterate over the array
for(int i=0; i<n; i++) {
for(int j=i+1; j<n; j++) {
// Fix two elements i and j
int remaining = target - arr[i] - arr[j];
// Apply two pointer approach on the remaining array
int left = j + 1, right = n - 1;
// Find the remaining two elements
while(left < right) {
// If the sum of two elements is equal to remaining
if(arr[left] + arr[right] == remaining) {
// Return "Yes"
return "Yes";
}
// If the sum of two elements is greater than remaining
if(arr[left] + arr[right] > remaining) {
// Decrement right
right--;
// If the sum of two elements is less than remaining
} else {
// Increment left
left++;
}
}
}
}
// If no such quadruplet is found, return "No"
return "No";
}