-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy pathtriplet_sum.cpp
61 lines (54 loc) · 1.67 KB
/
triplet_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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/******************************************************************************
Given an array arr of size n and an integer X.
Find if there's a triplet in the array which sums up to the given integer X.
*******************************************************************************/
//SOLUTION (in C++)
#include <bits/stdc++.h>
using namespace std;
void findTripletSum(int A[], int n, int X)
{
sort(A, A+n); //sort the array, it becomes easy to take the sum.
int i, j, k, count = 0;
for(i = 0;i<=n-3;i++) //i should only run till n-3 as we need three numbers for a triplet.
{
j = i+1;
k = n-1;
while(j<k) //two-pointer appraoch is used.
{
if(A[i] + A[j] + A[k] < X) //since the array is sorted, if the sum if less than X, the we need to increment j.
{
j++;
}
else if(A[i] + A[j] + A[k] > X) //If the sum is greater than X, we need to decrement k.
{
k--;
}
else if(A[i] + A[j] + A[k] == X) //If the sum is equal to X we need to increement j and decrement k, so that we may find other combinations of the given sum.
{
j++;
k--;
count++;
}
}
}
if(count == 0)
cout<<"The triplet doesn't exists"<<endl;
else
cout<<"The triplet exists"<<endl;
}
int main()
{
int n, X;
cout<<"Enter n"<<endl;
cin>>n;
cout<<"Enter the sum you want to find"<<endl;
cin>>X;
cout<<"Enter the array elements"<<endl;
int A[n];
for(int i = 0;i<n;i++)
{
cin>>A[i];
}
findTripletSum(A, n, X);
return 0;
}