Skip to main content

Tuple with Same Product - Solution & Explanation

MediumArrayHash TableCounting20 min readAsked at: Amazon, Microsoft, Meta +2
Practice this problem

Problem Statement

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] <= 104
  • All elements in nums are distinct.

Approach Overview

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.

Approach 1: Hashmap Based Approach

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

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.

Try this approach in the editor β†’

Approach 2: Two Pointers Approach

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

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.

Try this approach in the editor β†’

Approach 3: Combination + Hash Table

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.

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor β†’

Complexity Comparison

ApproachComplexity
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β€”

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Hashmap Based ApproachO(n^2)O(n^2)General case. Best performance when enumerating pair products and grouping them efficiently.
Two Pointers ApproachO(n^3)O(1)Useful when minimizing extra memory is required or when exploring pointer-based pair searching after sorting.

Video Solution

Tuple with Same Product - Leetcode 1726 - Python β€’ NeetCodeIO β€’ 12,307 views views

Watch 9 more video solutions β†’

Frequently Asked Questions

Is Tuple with Same Product easy or hard?
Tuple with Same Product is generally classified as a medium difficulty problem. The challenge lies in recognizing that equal products between pairs can be grouped and counted instead of checking every possible quadruple.
Tuple with Same Product Python/Java solution
Most implementations compute all pair products and store counts in a dictionary or hash map. Python uses a dictionary, Java uses HashMap, and C++ typically uses unordered_map. After counting frequencies, the result is calculated using the combination formula and multiplied by 8 for tuple permutations.
How to solve Tuple with Same Product in O(n)?
An O(n) solution is not achievable because the problem fundamentally depends on examining pair relationships between elements. At minimum, all O(n^2) pairs must be considered to detect equal products. The best practical approach uses a hash map to process these pairs efficiently in O(n^2) time.
What is the best approach for Tuple with Same Product?
The hash map pair-product approach is the most efficient. Compute the product for every pair of elements and store its frequency in a hash table. If a product appears k times, it forms k choose 2 pair combinations, and each combination produces 8 valid tuples. This results in O(n^2) time and O(n^2) space complexity.
Is Tuple with Same Product asked at Google/Amazon/Meta?
Tuple with Same Product represents a common interview pattern involving pair enumeration and hash maps. Variations of this problem appear in interviews at companies like Amazon, Google, and Meta where candidates are expected to recognize product collisions and optimize pair counting.
What data structure is used in Tuple with Same Product?
A hash table (hash map) is the primary data structure. It stores the frequency of each product generated by element pairs. This enables constant-time lookups and allows counting how many pair combinations share the same product.
What is the time complexity of Tuple with Same Product?
The optimal solution runs in O(n^2) time because it iterates through all pairs in the array and records their products in a hash map. Space complexity is also O(n^2) in the worst case due to storing pair product frequencies.

Ready to solve this problem?

Practice Tuple with Same Product with our built-in code editor and test cases.

Practice on FleetCode