Tuple with Same Product

Medium Topics Company Tags

The array nums contains distinct positive integers. Count the ordered tuples (a, b, c, d) whose four values are distinct elements of nums and satisfy a * b = c * d. Different orders count as different tuples. Return the total count.

Example 1:

Input: nums = [1,2,3,6]

Output: 8

Explanation: The pairs (1,6) and (2,3) both have product 6. Swapping the two pairs and choosing either order within each pair produces 2 * 2 * 2 = 8 ordered tuples.

Example 2:

Input: nums = [1,2,4,8,16]

Output: 24

Explanation: There are three equal-product pairings using four distinct values: (1,8) with (2,4), (1,16) with (2,8), and (2,16) with (4,8). Each contributes 8 tuples, for a total of 24.

Example 3:

Input: nums = [1,2,3]

Output: 0

Explanation: There are fewer than four distinct values, so no valid tuple can be formed.

Constraints:

  • 1 <= nums.length <= 1000
  • 1 <= nums[i] <= 10000
  • All values are distinct.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.