Skip to content

Instantly share code, notes, and snippets.

@mk6619
Last active December 1, 2019 04:24
Show Gist options
  • Select an option

  • Save mk6619/4515b951fd967c8f8e0557204c2868cc to your computer and use it in GitHub Desktop.

Select an option

Save mk6619/4515b951fd967c8f8e0557204c2868cc to your computer and use it in GitHub Desktop.
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int testCases, arrayLength;
cin>>testCases;
for(int i = 0; i < testCases; i++) {
cin>>arrayLength;
int arr[arrayLength];
for(int j = 0; j < arrayLength; j++) {
cin>>arr[j];
}
sort(arr,arr+arrayLength);
int triplets = 0;
for(int j = arrayLength-1; j > 0; j--) {
int pointer = 0 , right = j-1;
while(pointer < right) {
if((arr[right] + arr[pointer]) == arr[j]) {
triplets++;
right--;
pointer++;
}
else if ((arr[right] + arr[pointer]) < arr[j]) {
pointer++;
}
else {
right--;
}
}
}
if (triplets!=0) {
cout<<triplets<<"\n";
} else {
cout<<-1<<"\n";
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment