Given an array nums 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 nums, and a != b != c != d.

&nbsp;
Example 1:

Input: nums = [2,3,4,6]
Output: 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)


Example 2:

Input: nums = [1,2,4,5,10]
Output: 16
Explanation: There are 16 valid tuples:
(1,10,2,5) , (1,10,5,2) , (10,1,2,5) , (10,1,5,2)
(2,5,1,10) , (2,5,10,1) , (5,2,1,10) , (5,2,10,1)
(2,10,4,5) , (2,10,5,4) , (10,2,4,5) , (10,2,5,4)
(4,5,2,10) , (4,5,10,2) , (5,4,2,10) , (5,4,10,2)


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 1000
	1 &lt;= nums[i] &lt;= 104
	All elements in nums are distinct.


class Solution:
    def tupleSameProduct(self, nums: List[int]) -> int:

        from itertools import combinations
        mydict=defaultdict(int)
        ans=0

        for a,b in combinations(nums,2):
            mydict[a*b]+=1

        for i,j in mydict.items():
            if j>1:
                ans+=(j*(j-1)//2)*8

        return ans
class Solution {
    public int tupleSameProduct(int[] nums) {
        
        if(nums.length < 4){
            return 0;
        }
        
        int res = 0;
        
        HashMap<Integer, Integer> map = new HashMap<>();
        
        for(int i = 0; i < nums.length - 1; i++){
            
            for(int j = i + 1; j < nums.length; j++){
                
                int val = nums[i] * nums[j];
                map.put(val, map.getOrDefault(val, 0) + 1);
            }
        }
        
        for(int key : map.keySet()){
            
            int val = map.get(key);
            
            if(val > 1){
                res += val * (val - 1) * 4;    // (val * (val - 1) / 2) * 8
            }
        }
        
        return res;
    }
}
class Solution {
public:
    int tupleSameProduct(vector<int>& nums) {
        int n = nums.size();
        unordered_map<int,int> map;
        int res = 0;
        for(int i = 0 ; i < n ; i++){
            for(int j = i+1 ; j < n ; j++){
                int prod = nums[i] * nums[j];
                map[prod]++;//store product of each possible pair
            }
        }
        for(pair<int,int> m:map){
            int n=m.second;
            res += (n*(n-1))/2; //no. of tuple
        }
        return res*8; //Every tuple has 8 permutations
    }
};
var tupleSameProduct = function(nums) {
    let tupleCount = 0;
    let products = {}; // we'll keep track of how many times we've seen a given product before
    for (let a = 0; a < nums.length; a++) {
        for (let b = a + 1; b < nums.length; b++) {
            let product = nums[a] * nums[b];
            if (products[product]) { // we've seen at least one other pair of numbers with the same product already
                tupleCount += 8 * products[product]; // multiply by 8 because for any 4 numbers there are 8 permutations
                products[product] += 1; // increment the count, if we see this product again there are even more possible tuple combinations
            } else {
                products[product] = 1; // mark as seen once
            }
        }
    }
    return tupleCount;
};
Tuple with Same Product