forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_2176.java
31 lines (29 loc) · 993 Bytes
/
_2176.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
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class _2176 {
public static class Solution1 {
public int countPairs(int[] nums, int k) {
Map<Integer, List<Integer>> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
List<Integer> list = map.getOrDefault(nums[i], new ArrayList<>());
list.add(i);
map.put(nums[i], list);
}
int ans = 0;
for (int key : map.keySet()) {
List<Integer> list = map.get(key);
for (int i = 0; i < list.size() - 1; i++) {
for (int j = i + 1; j < list.size(); j++) {
if ((list.get(i) * list.get(j)) % k == 0) {
ans++;
}
}
}
}
return ans;
}
}
}