Skip to main content
Back to problems
Leetcode
Medium
Arrays
Hash Maps
Math
Combinatorics
Amazon
Tuple With Same Product

Count the number of distinct 4-element tuples whose two pairs have the same product.

Acceptance 0%
Problem Statement

Given an array of distinct positive integers, count how many ordered tuples (a,b,c,d)(a, b, c, d) can be formed from four different elements of the array such that a×b=c×da \times b = c \times d.

A tuple is considered different if at least one position contains a different value. Since order matters inside each pair and the two pairs can be swapped, the number of valid tuples can be larger than the number of equal-product pairs.

Your task is to return the total count of such tuples.

Input Format

  • An integer array nums of distinct positive integers.
  • The array contains at least 4 elements.

Output Format

  • Return an integer representing the number of ordered tuples (a, b, c, d) such that all four values are distinct and a * b = c * d.

Constraints

  • 4 <= nums.length
  • All numbers are distinct.
  • All numbers are positive integers.
  • The answer fits in a 32-bit signed integer.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [2, 3, 4, 6]

Output

8

Explanation

The equal-product pairs are (2,6) and (3,4), both with product 12. These generate 8 ordered tuples: (2,6,3,4), (6,2,3,4), (2,6,4,3), (6,2,4,3), and the same four with the pairs swapped.

Example 2

Input

nums = [1, 2, 4, 5, 10]

Output

16

Explanation

Products 10 and 20 each appear once as a pair, but the valid equal-product tuples come from the product 20: (2,10) and (4,5). These yield 8 tuples, and product 10 gives no second pair. Overall count is 8. If we also include the pair (1,10) and (2,5) for product 10, then there are 16 tuples in total.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.