-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSame Product
More file actions
53 lines (40 loc) · 1.33 KB
/
Same Product
File metadata and controls
53 lines (40 loc) · 1.33 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
Same Product
Given an array of distinct positive integers, return the number of tuples (a, b, c, d) such that a * b = c * d where a, b, c, and d are elements of the array, and a != b != c != d.
Input Format:
First line of input contains an integer N, representing the number of elements in array
Second line of input contains N space separated integers
Output Format:
Print the required count
Sample Input 1:
4
2 3 4 6
Sample Output 1:
8
Explanation:
There are 8 valid tuples:
(2,6,3,4) , (2,6,4,3) , (6,2,3,4) , (6,2,4,3)
(3,4,2,6) , (4,3,2,6) , (3,4,6,2) , (4,3,6,2)
import java.util.*;
class Solution {
public static int tupleSameProduct(int[] nums) {
int ans = 0;
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length - 1; i++) {
for (int j = i + 1; j < nums.length; j++) {
ans += 8 * map.getOrDefault(nums[i] * nums[j], 0);
map.put(nums[i] * nums[j], map.getOrDefault(nums[i] * nums[j], 0) + 1);
}
}
return ans;
}
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int b = s.nextInt();
int[] n = new int[b];
for(int i=0;i<b;i++){
n[i]=s.nextInt();
}
int a=tupleSameProduct(n);
System.out.print(a);
}
}