Pair Product

Pair Product

Given a slice of integers and a nonzero target, return the indices of the two numbers whose product equals the target. As with Pair Sum, scan left to right and return [i, j] for the earliest earlier index i that completes the pair at j, in increasing order. Assume exactly one valid answer exists.

pairProduct(nums, target)

Input format: nums | target. Output the two indices separated by a space.

Examples

pairProduct([]int{3, 2, 5, 4}, 8)   // → [1, 3]   (2 * 4)
pairProduct([]int{2, 7, 3}, 21)     // → [1, 2]   (7 * 3)
pairProduct([]int{6, 4}, 24)        // → [0, 1]

Walkthrough

Thinking it through

This is the same shape as finding a pair that sums to a target, just with multiplication instead of addition. If you already know how to solve the sum version, you can carry the same reasoning over almost unchanged — just swap the operation and its inverse.

Reframing around the inverse operation

For a sum target, the value you need to have already seen is target minus current. For a product target, it's target divided by current — division undoes multiplication the same way subtraction undoes addition. So the core idea carries over: scan left to right and ask "have I already seen the partner this number needs?", where "partner" is defined by the inverse operation.

Why a hash map still works

Checking every pair of indices is O(n²) — the same waste as before. By the time you reach a given index, you already know every value before it; you just need a fast way to check if a specific value was among them. A hash map from value to its first index gives you that O(1) lookup, turning the nested loop into a single pass.

The extra care multiplication requires

Division introduces two wrinkles addition never had:

  1. Division by zero. If the current number is zero, you can't divide the target by it. Zero can only be part of a valid pair when the target itself is zero and another zero shows up elsewhere — a different check entirely. So skip zero values when looking for a partner via division.
  2. Non-integer partners. Even for a nonzero current number, target / current might not be a whole number. Since you're working with integers, only compute and look up the quotient when the current value evenly divides the target.

Building the approach

  1. Walk the list once, tracking the current index.
  2. Before recording anything: is the current value nonzero and does it evenly divide the target? If so, compute the needed factor and check whether it's already in your map — a hit means you've found your pair.
  3. If not, record the current value's index (only if not already present, so the earliest index wins for repeats), and continue.
  4. Because the lookup happens before the insert, you never pair a value with itself.

Why this generalizes

Many "two elements that combine to a target" problems reduce to the same pattern: find the inverse of the combining operation, use it to compute the partner you need, and check for that partner with a hash map instead of rescanning. The one extra step here is respecting division's edge cases — when it's undefined, or when it can't produce a valid integer answer.

main.py
Console
Press Run to execute your code, or Submit to test and complete this problem.