- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1726.java
41 lines (38 loc) · 1.34 KB
/
_1726.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
packagecom.fishercoder.solutions.secondthousand;
importjava.util.HashMap;
importjava.util.Map;
publicclass_1726 {
publicstaticclassSolution1 {
publicinttupleSameProduct(int[] nums) {
Map<Integer, Integer> map = newHashMap<>();
intcount = 0;
for (inti = 0; i < nums.length - 1; i++) {
for (intj = i + 1; j < nums.length; j++) {
intproduct = nums[i] * nums[j];
count += 8 * map.getOrDefault(product, 0);
map.put(product, map.getOrDefault(product, 0) + 1);
}
}
returncount;
}
}
publicstaticclassSolution2 {
publicinttupleSameProduct(int[] nums) {
Map<Integer, Integer> map = newHashMap<>();
for (inti = 0; i < nums.length - 1; i++) {
for (intj = i + 1; j < nums.length; j++) {
intproduct = nums[i] * nums[j];
map.put(product, map.getOrDefault(product, 0) + 1);
}
}
intcount = 0;
for (intkey : map.keySet()) {
if (map.get(key) > 1) {
intfreq = map.get(key);
count += (freq * (freq - 1) / 2) * 8;
}
}
returncount;
}
}
}