-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtripletSumArray.cpp
More file actions
51 lines (48 loc) · 957 Bytes
/
tripletSumArray.cpp
File metadata and controls
51 lines (48 loc) · 957 Bytes
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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int tripletSum(int arr[],int n, int k){
int flag=0;
if(n<3){
return flag;
}
for(int i=0;i<n-2;i++){
int l=i+1;
int r=n-1;
int sum=0;
while(l<r){
sum=arr[i]+arr[l]+arr[r];
if(sum==k){
flag=1;
break;
}
else if(sum<k){
l++;
}
else{
r--;
}
}
}
return flag;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int t;
cin>>t;
while (t--){
int n,k;
cin>>n>>k;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
sort(arr,arr+n);
int res=tripletSum(arr,n,k);
cout<<res<<endl;
}
return 0;
}