-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPossibleGroups.java
More file actions
50 lines (45 loc) · 1.59 KB
/
PossibleGroups.java
File metadata and controls
50 lines (45 loc) · 1.59 KB
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
/**
* Created by MalhotR1 on 04/25/2017.
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class PossibleGroups {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine().trim());
for (int t = 0; t < T; t++) {
int N = Integer.parseInt(br.readLine().trim());
String[] in = br.readLine().trim().split(" ");
int[] arr = new int[in.length];
Set<Integer> set = new HashSet<>();
for (int i = 0; i < in.length; i++) {
arr[i] = Integer.parseInt(in[i]);
if (arr[i] % 3 == 0){
if (set.contains(arr[i])){ arr[i] = -1; }
else set.add(arr[i]);
}
}
int total = calculateTotal(set.size());
Arrays.sort(arr);
for (int i = 0; i < arr.length; i++) {
if (arr[i] < 0 || set.contains(arr[i])) continue;
else {
for (int j = i + 1; j < arr.length; j++) {
if ((arr[i] + arr[j]) % 3 == 0) {
total += 1 + set.size();
}
}
}
}
System.out.println(total);
}
}
private static int calculateTotal(int N) {
return ((N+1) * N * (N-1))/6;
}
}