Product of Array Except Self

MediumPrefix and suffix productsLeetCode 238 ↗World 2-13
0:00 / 0:00

Each spot gets the product of every other number, without division. Multiply everything to its left by everything to its right in two passes.

▼

The problem

LeetCode 238 (Medium). Given an array nums, return answer where answer[i] is the product of every element except nums[i], in O(n) time and without using division.

Example: [1, 2, 3, 4] → [24, 12, 8, 6].

def productExceptSelf(nums):
    n = len(nums)
    answer = [1] * n
    left = 1
    for i in range(n):
        answer[i] = left      # store
        left *= nums[i]       # multiply in
    right = 1
    for i in range(n - 1, -1, -1):
        answer[i] *= right    # into the answer
        right *= nums[i]      # multiply in
    return answer
TRY IT ON LEETCODE ▶

The solution

def productExceptSelf(nums):
    n = len(nums)
    answer = [1] * n
    left = 1
    for i in range(n):
        answer[i] = left      # store
        left *= nums[i]       # multiply in
    right = 1
    for i in range(n - 1, -1, -1):
        answer[i] *= right    # into the answer
        right *= nums[i]      # multiply in
    return answer

Transcript

Product of Array Except Self. Given a list of numbers, return a list where each spot holds the product of all the others. In linear time, without division.

Take one, two, three, four. Skip the one: two times three times four is twenty-four. Skip the two: twelve. Skip the three: eight. Skip the four: six.

The simple way multiplies all the others for every spot: n times n work. Dividing the total by each number is quick, but not allowed, and one zero breaks it. The total becomes zero, and the zero's own spot would need zero divided by zero.

Here's the trick. Everything except one spot splits in two: the numbers on its left, and the numbers on its right. For the three: the left is one times two, the right is four. Two times four is eight. So send a beam from the left that multiplies as it passes each number, and one from the right. Each spot multiplies what reaches it from both sides.

In code, the answer starts as all ones. The first loop runs left to right with a running product: at each spot, store it, then multiply in that number. The second loop runs right to left: multiply a new running product into the answer, then multiply in the number. Store first, so a spot never counts itself.

Run it. From the left, starting at one: it leaves one, picks up one. Leaves one, picks up two. Leaves two, picks up three, and leaves six: the product before each spot. Now from the right, starting at one. The last spot: six times one, six. The beam picks up four: two times four, eight. Picks up three: one times twelve, twelve. Then twenty-four. The answer: twenty-four, twelve, eight, six.

Two passes, so the time is linear. Besides the answer, we keep one running product: constant extra space. And with no division, zeros are fine: three, zero, four gives zero, twelve, zero.

Left times right, no division. That's Product of Array Except Self.