Count the number of distinct 4-element tuples whose two pairs have the same product.
Given an array of distinct positive integers, count how many ordered tuples can be formed from four different elements of the array such that .
A tuple is considered different if at least one position contains a different value. Since order matters inside each pair and the two pairs can be swapped, the number of valid tuples can be larger than the number of equal-product pairs.
Your task is to return the total count of such tuples.
Input Format
- An integer array
numsof distinct positive integers. - The array contains at least 4 elements.
Output Format
- Return an integer representing the number of ordered tuples
(a, b, c, d)such that all four values are distinct anda * b = c * d.
Constraints
4 <= nums.length- All numbers are distinct.
- All numbers are positive integers.
- The answer fits in a 32-bit signed integer.
Example 1
Input
nums = [2, 3, 4, 6]
Output
8
Explanation
The equal-product pairs are (2,6) and (3,4), both with product 12. These generate 8 ordered tuples: (2,6,3,4), (6,2,3,4), (2,6,4,3), (6,2,4,3), and the same four with the pairs swapped.
Example 2
Input
nums = [1, 2, 4, 5, 10]
Output
16
Explanation
Products 10 and 20 each appear once as a pair, but the valid equal-product tuples come from the product 20: (2,10) and (4,5). These yield 8 tuples, and product 10 gives no second pair. Overall count is 8. If we also include the pair (1,10) and (2,5) for product 10, then there are 16 tuples in total.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.