Skip to content
BytePatterns

Product Of Others

MediumArrays#prefix-products#two-pass~25m

Problem

Given a list of integers, build a new list of the same length where each position holds the product of every other value in the input. The value at that position itself is left out of its own product. Solve it without using division, since a single zero in the input would make division impossible.

Examples

Input:  nums = [2, 3, 4, 5]
Output: [60, 40, 30, 24]
Why:    3*4*5, 2*4*5, 2*3*5, 2*3*4
Input:  nums = [1, 0, 3]
Output: [0, 3, 0]
Why:    only the slot facing the zero escapes it
Input:  nums = [0, 0, 7]
Output: [0, 0, 0]
Why:    edge case, two zeros wipe out every position

Hints

0 / 3

Stuck on the idea rather than the code? Prefix Sums covers it.