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.
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)
Constraints:
1 <= nums.length <= 10001 <= nums[i] <= 104nums are distinct.Problem Overview: You receive an array of distinct integers. The task is to count the number of tuples (a, b, c, d) such that a * b = c * d and all four elements come from different indices. Order matters, so each valid pair combination generates multiple permutations of tuples.
Approach 1: Hashmap Based Approach (O(n^2) time, O(n^2) space)
The key observation: if two different pairs of numbers produce the same product, they can form valid tuples. Iterate through all pairs (i, j) with i < j and compute their product. Store the frequency of each product in a hash map. If a product appears k times, it means there are k pairs producing that product. Any two of these pairs can combine to form tuples. The number of pair combinations is k choose 2, and each pair combination generates 8 valid permutations of (a, b, c, d). This approach relies on fast hash lookups and pair enumeration, making it the optimal method using a hash table and counting technique.
Approach 2: Two Pointers Approach (O(n^3) time, O(1) space)
Sort the array first. Then iterate through every pair (i, j) to form a target product. For the remaining elements, attempt to find another pair whose product matches the target using a two‑pointer scan from both ends of the remaining subarray. Each time a matching pair is found, count the valid tuple permutations. This method avoids additional memory but repeatedly scans the array for matching products, which increases the runtime significantly compared to the hashing approach.
Recommended for interviews: The hashmap approach is what interviewers expect. It demonstrates recognition of pair-product collisions and reduces the search space from brute-force tuple enumeration to pair counting. Brute-force or pointer-based exploration shows understanding of the problem structure, but the O(n^2) hash map solution highlights strong pattern recognition and efficient use of data structures.
In this approach, we will use a hashmap (dictionary) to keep track of products of each pair. The key will be the product, and the value will be the number of ways a particular product can be formed. We will iterate over each unique pair of numbers in the array, compute their product, and store the count of each product in the hashmap. For every pair, the count in the hashmap will give the number of tuples that can be formed with this product. For every new product count, the number of tuples formed can be calculated using combinatorial arithmetic logic (multiplying counts of previously seen pairs forming the same product).
This C solution uses an array to simulate a hashmap we utilize as a product counter. We iterate over all pairs, compute the product, and check how many pairs have previously resulted in the same product. For each such pair, there are 8 tuples that can be formed.
The time complexity is O(N^2), where N is the length of the array, as we check all pairs. The space complexity is O(U), where U is the maximum possible product of two numbers, due to the product counter array.
This approach aims to reduce time complexity by first sorting the array, and leveraging the two-pointer technique to find pairs with the same product more efficiently. Once the array is sorted, for each pair, the pointers can be adjusted based on whether the product is less than or greater than the target product for current analysis.
This solution uses sorting followed by a two-pointer method to find pairs. By iterating each pair and adjusting the pointers based on comparisons with the target product, the solution becomes more efficient in certain conditions than a simple brute-force.
The time complexity is O(N^3) in the worst case, factoring in sorting and two-pointer traversal per pair. Space complexity is O(1) apart from sorting requirements.
Assuming there are n pairs of numbers, for any two pairs of numbers a, b and c, d that satisfy the condition a times b = c times d, there are a total of C_n^2 = \frac{n times (n-1)}{2} such combinations.
According to the problem description, each combination that satisfies the above condition can form 8 tuples that satisfy the problem requirements. Therefore, we can multiply the number of combinations with the same product by 8 (equivalent to left shifting by 3 bits) and add them up to get the result.
The time complexity is O(n^2), and the space complexity is O(n^2). Here, n is the length of the array.
| Approach | Complexity |
|---|---|
| Hashmap Based Approach | The time complexity is O(N^2), where N is the length of the array, as we check all pairs. The space complexity is O(U), where U is the maximum possible product of two numbers, due to the product counter array. |
| Two Pointers Approach | The time complexity is O(N^3) in the worst case, factoring in sorting and two-pointer traversal per pair. Space complexity is O(1) apart from sorting requirements. |
| Combination + Hash Table | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Hashmap Based Approach | O(n^2) | O(n^2) | General case. Best performance when enumerating pair products and grouping them efficiently. |
| Two Pointers Approach | O(n^3) | O(1) | Useful when minimizing extra memory is required or when exploring pointer-based pair searching after sorting. |
Tuple with Same Product - Leetcode 1726 - Python • NeetCodeIO • 12,247 views views
Watch 9 more video solutions →Practice Tuple with Same Product with our built-in code editor and test cases.
Practice on FleetCode