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 <= 1000
1 <= nums[i] <= 10^4
nums
are distinct.# Optimal Solution
This problem requires us to find the number of tuples (a, b, c, d) such that a * b = c * d, where a, b, c, and d are distinct elements from the input array `nums`. The brute-force approach would involve checking all possible combinations, which is highly inefficient. The optimal solution involves using a hash map to store the frequency of each product of pairs and then calculating the number of valid tuples based on these frequencies.
## Code
```python
from collections import defaultdict
def tuple_same_product(nums):
product_counts = defaultdict(int)
count = 0
n = len(nums)
for i in range(n):
for j in range(i + 1, n):
product = nums[i] * nums[j]
product_counts[product] += 1
for product in product_counts:
freq = product_counts[product]
count += freq * (freq - 1) * 4
return count
Example:
nums = [2, 3, 4, 6]
result = tuple_same_product(nums)
print(result) # Output: 8
nums = [1, 2, 4, 5, 10]
result = tuple_same_product(nums)
print(result) # Output: 16
n
times, and the inner loop iterates n - i - 1
times, which is approximately n
times in the worst case. Therefore, computing products and updating the hash map takes O(n^2) time.k
is the number of unique products. In the worst case, k
can be O(n^2), but on average, it will be less.product_counts
hash map, which stores the frequency of each unique product.nums
is empty, the function should return 0, as there are no tuples to form.a * b
could exceed the maximum integer value, leading to overflow issues. To handle this, you might consider using larger data types (e.g., long
in Java, int64
in Python) or scaling the numbers down.A brute-force solution would involve iterating through all possible combinations of four elements (a, b, c, d) and checking if a * b = c * d. This approach is highly inefficient, especially for large input arrays, due to its high time complexity.
def tuple_same_product_naive(nums):
count = 0
n = len(nums)
for i in range(n):
for j in range(n):
if i == j:
continue
for k in range(n):
if k == i or k == j:
continue
for l in range(n):
if l == i or l == j or l == k:
continue
if nums[i] * nums[j] == nums[k] * nums[l]:
count += 1
return count
Time Complexity: O(n^4) Space Complexity: O(1)
This solution is not practical for large input arrays due to its high time complexity.