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