-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounttriplets.java
More file actions
37 lines (35 loc) · 852 Bytes
/
counttriplets.java
File metadata and controls
37 lines (35 loc) · 852 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
/*package whatever //do not write package name here */
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) {
//code
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-- != 0){
int n = s.nextInt();
int ar[] = new int[n];
for(int i=0;i<n;i++) ar[i] = s.nextInt();
Arrays.sort(ar);
int count = 0;
for(int i=n-1;i>=0;i--){
int j=0;
int k = i-1;
while(j<k){
if(ar[i] == ar[k]+ar[j]){
count++;
j++;
k--;
}
else if(ar[i] > ar[k]+ar[j]){
j++;
}
else k--;
}
}
if(count == 0) System.out.println("-1");
else System.out.println(count);
}
}
}