-
Notifications
You must be signed in to change notification settings - Fork 871
/
Copy pathCountTriplets.java
64 lines (49 loc) · 1.46 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
*
* Problem Statement-
* [Count Triplets](https://www.hackerrank.com/challenges/count-triplets-1/problem)
* [Tutorial](https://youtu.be/tBFZMaWP0W8)
*
*/
package com.javaaid.dictionaries;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class CountTriplets {
private static long countTriplets(List<Long> arr, long r) {
Map<Long, Long> leftMap = new HashMap<>();
Map<Long, Long> rightMap = new HashMap<>();
for (long item : arr) {
rightMap.put(item, rightMap.getOrDefault(item, 0L) + 1);
}
long count = 0;
for (int i = 0; i < arr.size(); i++) {
long midTerm = arr.get(i);
long c1 = 0, c3 = 0;
rightMap.put(midTerm, rightMap.getOrDefault(midTerm, 0L) - 1);
if (leftMap.containsKey(midTerm / r) && midTerm % r == 0) {
c1 = leftMap.get(midTerm / r);
}
if (rightMap.containsKey(midTerm * r))
c3 = rightMap.get(midTerm * r);
count += c1 * c3;
leftMap.put(midTerm, leftMap.getOrDefault(midTerm, 0L) + 1);
}
return count;
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
long n = sc.nextLong();
long r = sc.nextLong();
List<Long> arr = new ArrayList<>();
while (n-- > 0) {
arr.add(sc.nextLong());
}
long ans = countTriplets(arr, r);
System.out.println(ans);
sc.close();
}
}