1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Solution { public int tupleSameProduct(int[] nums) { int tupleCount = 0;
Map<Integer, Integer> productToCountMap = new HashMap<>(); for (int i = 0; i < nums.length - 1; i++) { for (int j = i + 1; j < nums.length; j++) { int product = nums[i] * nums[j]; int count = productToCountMap.getOrDefault(product, 0); tupleCount += count * 8;
productToCountMap.put(product, count + 1); } }
return tupleCount; } }
|
References
1726. Tuple with Same Product