Program to find tuple with same product in Python

Given an array nums with unique positive values, we need to find 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 all elements are distinct.

For example, if the input is nums = [2,3,4,6], then the output will be 8 because we can get tuples like (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).

Algorithm

To solve this problem, we will follow these steps ?

  • Create a dictionary to store the frequency of products of all pairs
  • Initialize ans = 0
  • For each pair (i, j) where i < j, calculate nums[i] * nums[j] and increment its count
  • For each product that appears more than once, calculate the number of valid tuples
  • Return the total count

Example

Let us see the following implementation to get better understanding ?

from collections import defaultdict

def solve(nums):
    dic = defaultdict(int)
    ans = 0
    
    # Count frequency of products of all pairs
    for i in range(len(nums) - 1):
        for j in range(i + 1, len(nums)):
            dic[nums[i] * nums[j]] += 1
    
    # Calculate number of tuples for each product
    for v in dic.values():
        if v == 1:
            continue
        # For n pairs with same product, number of tuples = n * (n-1) * 4
        # Each pair can be arranged in 4 ways: (a,b,c,d), (a,b,d,c), (b,a,c,d), (b,a,d,c)
        v = v - 1
        s = (v / 2) * (8 + 8 * v)
        ans += s
    
    return int(ans)

# Test with the given example
nums = [2, 3, 4, 6]
result = solve(nums)
print(f"Input: {nums}")
print(f"Output: {result}")
Input: [2, 3, 4, 6]
Output: 8

How It Works

The algorithm works by first finding all pairs and their products. For the array [2, 3, 4, 6]:

  • Pairs: (2,3)=6, (2,4)=8, (2,6)=12, (3,4)=12, (3,6)=18, (4,6)=24
  • Product 12 appears twice: from pairs (2,6) and (3,4)
  • These two pairs can form 2 × (2-1) × 4 = 8 valid tuples

Testing with Different Input

# Test with a different example
nums2 = [1, 2, 4, 5, 10]
result2 = solve(nums2)
print(f"Input: {nums2}")
print(f"Output: {result2}")

# Check the products
dic = defaultdict(int)
for i in range(len(nums2) - 1):
    for j in range(i + 1, len(nums2)):
        product = nums2[i] * nums2[j]
        dic[product] += 1
        print(f"Pair ({nums2[i]}, {nums2[j]}) = {product}")

print(f"Product frequencies: {dict(dic)}")
Input: [1, 2, 4, 5, 10]
Output: 8
Pair (1, 2) = 2
Pair (1, 4) = 4
Pair (1, 5) = 5
Pair (1, 10) = 10
Pair (2, 4) = 8
Pair (2, 5) = 10
Pair (2, 10) = 20
Pair (4, 5) = 20
Pair (4, 10) = 40
Pair (5, 10) = 50
Product frequencies: {2: 1, 4: 1, 5: 1, 10: 2, 8: 1, 20: 2, 40: 1, 50: 1}

Conclusion

This solution efficiently finds tuples with the same product by counting pair frequencies and calculating valid arrangements. The time complexity is O(n²) for generating pairs and O(k) for counting tuples, where k is the number of unique products.

Updated on: 2026-03-26T14:26:43+05:30

567 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements