Count Equal and Divisible Pairs in an Array LeetCode solution in Java

1
class Solution {
2
public int countPairs(int[] nums, int k) {
3
HashMap<Integer, List<Integer>> hMap = new HashMap<>();
4
int count = 0;
5
for (int i = 0; i < nums.length; i++) {
6
if (!hMap.containsKey(nums[i])) {
7
List<Integer> l = new ArrayList<>();
8
l.add(i);
9
hMap.put(nums[i], l);
10
} else {
11
List<Integer> v = hMap.get(nums[i]);
12
for (Integer j : v) {
13
if ((i * j) % k == 0) count++;
14
}
15
v.add(i);
16
hMap.put(nums[i], v);
17
}
18
}
19
return count;
20
}
21
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0