forked from aman05382/DataStructures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3sum.cpp
77 lines (71 loc) · 1.64 KB
/
3sum.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
Problem: Given an array and target x, determine wether there are 3 indices i,j,k such that a[i]+a[j]+a[k]==x .
Complexity: Time:O(N^2) ,Space: O(N)
*/
#include <iostream>
#include <algorithm>
using namespace std;
void naive(int a[], int n, int x)
{
//brute force all 3 indices in O(N^3)
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
if (i != j && j != k && k != i)
{
if (a[i] + a[j] + a[k] == x)// found the answer
{
cout << "YES\n";
return;
}
}
}
}
}
cout << "NO\n";
}
void efficient(int a[], int n, int x)
{
//alternatively , we can sort the array, fix j and try to find i and k;
// this will be O(NlogN)+O(N^2)= O(N^2)
sort(a, a + n); // sorting the array .
for (int j = 1; j < n - 1; j++)
{
/*
for each index j, we are trying to find i to its left and k to its right .
*/
int i = 0, k = n - 1; // we will use 2 pointers i and k.
while (i < j && j < k)
{
if (a[i] + a[k] == x - a[j]) // we have found a suitable triplet.
{
cout << "YES\n";
return;
}
else if (a[i] + a[k] > x - a[j]) //=> a[i]+a[j]+a[k] > x ,now since a is already sorted, to decrease the value of sum, we decrease value of a[k] => we decrease k.
{
k--;
}
else //=> a[i]+a[j]+a[k] < x ,now since a is already sorted, to increase the value of sum, we increase value of a[i] => we increase i.
{
i++;
}
}
}
cout << "NO\n";
}
int main()
{
int n, x;
cin >> n >> x;
int a[n];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
efficient(a, n, x);
return 0;
}