Program to find number of quadruples for which product of first and last pairs are same in Python

Suppose we have a list of numbers called nums, with unique positive numbers. We have to find the number of quadruples like (a, b, c, d) from nums such that a*b = c*d, where a, b, c and d are all distinct elements of nums.

So, if the input is like nums = [3, 6, 4, 8], then the output will be 8, because the quadruples are [[3,8,6,4], [3,8,4,6], [8,3,6,4], [8,3,4,6], [6,4,3,8], [4,6,3,8], [6,4,8,3], [4,6,8,3]].

Algorithm

To solve this, we will follow these steps −

  • Create a dictionary c to store product frequencies
  • Calculate n as the size of nums
  • For each pair of indices (i, j) where i < j:
    • Calculate product x = nums[i] * nums[j]
    • Increment count for this product in dictionary
  • For each product that appears multiple times, calculate combinations
  • Multiply result by 4 to account for all permutations

Example

Let us see the following implementation to get better understanding −

def solve(nums):
    c = {}
    n = len(nums)
    
    # Count frequency of each product
    for i in range(n):
        for j in range(i + 1, n):
            x = nums[i] * nums[j]
            c[x] = c.get(x, 0) + 1
    
    # Calculate quadruples
    ret = 0
    for x in c.values():
        ret += x * (x - 1)
    
    return ret * 4

nums = [3, 6, 4, 8]
print(solve(nums))
8

How It Works

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

def solve_with_explanation(nums):
    c = {}
    n = len(nums)
    
    print("Finding all pairs and their products:")
    for i in range(n):
        for j in range(i + 1, n):
            x = nums[i] * nums[j]
            c[x] = c.get(x, 0) + 1
            print(f"Pair ({nums[i]}, {nums[j]}) has product {x}")
    
    print(f"\nProduct frequencies: {c}")
    
    ret = 0
    for product, count in c.items():
        combinations = count * (count - 1)
        ret += combinations
        if combinations > 0:
            print(f"Product {product} appears {count} times, contributes {combinations}")
    
    result = ret * 4
    print(f"\nTotal combinations: {ret} × 4 = {result}")
    return result

nums = [3, 6, 4, 8]
solve_with_explanation(nums)
Finding all pairs and their products:
Pair (3, 6) has product 18
Pair (3, 4) has product 12
Pair (3, 8) has product 24
Pair (6, 4) has product 24
Pair (6, 8) has product 48
Pair (4, 8) has product 32

Product frequencies: {18: 1, 12: 1, 24: 2, 48: 1, 32: 1}
Product 24 appears 2 times, contributes 2

Total combinations: 2 × 4 = 8

Key Points

  • We multiply by 4 because each valid pair of pairs can form 4 different quadruples (considering order)
  • Only products that appear more than once contribute to the result
  • The formula x * (x - 1) gives us the number of ways to choose 2 pairs from x pairs with the same product

Conclusion

This solution efficiently finds quadruples with equal products by counting pair products and calculating combinations. The time complexity is O(n²) for generating all pairs, and the algorithm multiplies by 4 to account for all possible arrangements of the quadruples.

---
Updated on: 2026-03-26T17:11:28+05:30

347 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements