-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountTriplets.java
48 lines (36 loc) · 1.19 KB
/
CountTriplets.java
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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
public class CountTriplets {
private static Scanner scanner = new Scanner(System.in);
static long countTriplets(List<Long> arr, long r) {
long count = 0;
HashMap<Long, Long> singleMap = new HashMap<Long, Long>();
HashMap<Long, Long> doubleMap = new HashMap<Long, Long>();
for( Long item : arr ) {
if ( item % r == 0 ) {
long preItem = item / r;
Long countTriplet = doubleMap.get(preItem);
if ( countTriplet != null ) {
count += countTriplet;
}
Long countPair = singleMap.get(preItem);
if (countPair != null) {
doubleMap.put(item, doubleMap.getOrDefault(item, 0L) + countPair);
}
}
singleMap.put(item, singleMap.getOrDefault(item, 0L) + 1);
}
return count;
}
public static void main(String[] args) {
int items = scanner.nextInt();
long ratio = scanner.nextLong();
ArrayList<Long> arr = new ArrayList<Long>(items);
for (int i = 0; i < items; i++) {
arr.add(scanner.nextLong());
}
System.out.println(countTriplets(arr, ratio));
}
}