← Whiteboard questions

Product of Array Except Self

Find the product of all the other numbers at each position

DSA, Medium, Arrays & Hashing

Which structure or algorithm, and what does it cost.

A list nums holds whole numbers. Return a list out of the same length, where out[i] is the product of every number in nums except nums[i]. Solve it without the divide operator. The list can hold zeros, and every answer fits in a 64-bit integer.

Examples

Input nums = [2, 3, 4, 5]
Output [60, 40, 30, 24]
Why out[0] is 3 * 4 * 5 = 60, out[1] is 2 * 4 * 5 = 40, and so on.
Input nums = [3, 0, 2]
Output [0, 6, 0]
Why only the position of the zero skips it, so only that position gets the product 3 * 2 = 6. All others include the zero.
Input nums = [-2, 5]
Output [5, -2]
Why with two numbers, each answer is the other number, and the sign stays.

Constraints

Your answer must contain

Target Aim for O(n) time and O(1) extra memory, not counting the answer list.

Hints

Try the problem first. Open one hint at a time.

Hint 1 a nudge

Pick one position. What do you need to know about the numbers on its left side, and about the numbers on its right side?

Hint 2 the approach

You can build the left products for all positions in one pass, and the right products in another. Then combine them.

Hint 3 the key step

Write the running product of the left side into the answer list. Then walk from the right with a running product and multiply it into each slot.

Walkthrough

Full walkthrough brute force to best

Brute force

For each position, multiply all the other numbers. Each position needs n - 1 multiplications, so the total is O(n^2) time and O(1) extra memory. For 100,000 numbers that is about 10 billion multiplications.

Better method

Build a list of left products, where left[i] is the product of nums before i, and a list of right products, where right[i] is the product of nums after i. Then out[i] is left[i] * right[i]. That is O(n) time and O(n) extra memory for the two lists.

Best method

Use the answer list itself for the left products, then fold the right products in with one running number. First pass, left to right: store in out[i] the product of everything before i, and update a running left product with nums[i]. Second pass, right to left: multiply out[i] by the running product of everything after i, then update that running product with nums[i]. Each position gets left times right, and the number at i is in neither. No division is needed, so zeros are safe. The extra memory is two numbers.

Hand trace

Run on [2, 3, 4, 5]. First pass with left starting at 1. i = 0: out[0] = 1, left = 2. i = 1: out[1] = 2, left = 6. i = 2: out[2] = 6, left = 24. i = 3: out[3] = 24, left = 120. Now out is [1, 2, 6, 24]. Second pass with right starting at 1. i = 3: out[3] = 24 * 1 = 24, right = 5. i = 2: out[2] = 6 * 5 = 30, right = 20. i = 1: out[1] = 2 * 20 = 40, right = 60. i = 0: out[0] = 1 * 60 = 60. The result is [60, 40, 30, 24].

Code

def product_except_self(nums):
    n = len(nums)
    out = [1] * n
    left = 1
    for i in range(n):
        out[i] = left
        left *= nums[i]
    right = 1
    for i in range(n - 1, -1, -1):
        out[i] *= right
        right *= nums[i]
    return out

Time and memory

O(n) time, because there are two passes with one multiplication each per position. O(1) extra memory beyond the answer list, because only the running left and right numbers are kept.

Common mistakes

  • Dividing the total product by each number. This breaks when the list holds a zero, and the question does not allow it.
  • Starting the running product at nums[0] or including nums[i] before storing it. The number at position i must not be in its own answer, so store first, then multiply it in.
  • Forgetting to update the running right product after you use it, so every position gets the same right side. Or starting a running product at 0 instead of 1, so every answer is 0.
  • In C++ or Java the last update of each running product can overflow. It is never used, so you may skip it.

Related problems

More Arrays & Hashing problems
Product of Array Except Self: walkthrough and solution