Tuple with Same Product - Problem

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.

All four elements must be different from each other, and their products must be equal.

Input & Output

Example 1 — Basic Case
$ Input: nums = [2,3,4,6]
Output: 8
💡 Note: We have 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). All satisfy a*b = c*d where 2*6 = 3*4 = 12.
Example 2 — Single Element Repeated Product
$ Input: nums = [1,2,4,5,10]
Output: 16
💡 Note: Product 10 appears 3 times: 1*10, 2*5, so we get 3*(3-1) = 6 tuples. Product 20 appears 2 times: 2*10, 4*5, so we get 2*(2-1) = 2 tuples. Total is 6+2 = 8 tuples, but accounting for all permutations gives 16.
Example 3 — No Valid Tuples
$ Input: nums = [2,3,5,7]
Output: 0
💡 Note: All numbers are prime, so no two different pairs can have the same product. Products are: 2*3=6, 2*5=10, 2*7=14, 3*5=15, 3*7=21, 5*7=35. All unique, so 0 tuples.

Constraints

  • 1 ≤ nums.length ≤ 1000
  • 1 ≤ nums[i] ≤ 104
  • All elements in nums are distinct

Visualization

Tap to expand
Tuple with Same Product: Find a*b = c*d2346Input: [2,3,4,6]2×6 = 123×4 = 12Same Product Found!2×6 = 3×4 = 12Can form tuplesValid 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)8total tuples
Understanding the Visualization
1
Input
Array of distinct positive integers
2
Process
Find all pairs with matching products
3
Output
Count of valid 4-element tuples
Key Takeaway
🎯 Key Insight: Count pairs by their product - if n pairs share the same product, they form n×(n-1) valid tuples
Asked in
Google 15 Facebook 12 Amazon 8
31.2K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen