Leave-One-Out Products Without Using Division
Arrays & HashingAuto-graded · 19 test casesLast updated
Build an array where each entry is the product of all other entries, without division, in linear time, using prefix and suffix running products.
Leave-One-Out Products
You are given a list of integers values. Produce a new list answer of the same length where answer[i] is the product of every element of values except the one at position i.
The division operator must not be used. Aim for linear time.
Rules
answer[i]multiplies together allvalues[j]withj != i.- Zeros and negative numbers are allowed and follow ordinary multiplication rules. For example, if the list contains exactly one zero, only the entry at that zero's position can be nonzero. If it contains two or more zeros, every entry is
0. - Return the resulting list of integers.
Constraints
2 <= len(values) <= 10^5-10 <= values[i] <= 10- Every prefix product and every suffix product fits in a signed 64-bit integer.
- Do not use division.
- Target
O(n)time andO(1)extra space, not counting the returned list.
Examples
Example 1
Input: leave_one_out_products([1, 2, 3, 4])
Output: [24, 12, 8, 6]
Example 2
Input: leave_one_out_products([-1, 1, 0, -3, 3])
Output: [0, 0, 9, 0, 0]
Hints
Hint 1
Split the product for index i into two parts: everything to its left and everything to its right.
Hint 2
Walk left to right keeping a running product, storing it in the output slot before you multiply the current element into it.
Hint 3
Walk right to left with a single running suffix variable, multiplying it into the output slot before folding in the current element. Zeros need no special handling.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The product of everything except values[i] is the product of the elements to its left times the product of the elements to its right. Both pieces can be computed with running products, so no division is needed.
- Left pass. Keep a running product starting at
1. For each indexifrom left to right, store the running product inanswer[i], then multiplyvalues[i]into it. After this pass,answer[i]is the product of all elements strictly left ofi. - Right pass. Reset the running product to
1. For each indexifrom right to left, multiplyanswer[i]by the running product, then multiplyvalues[i]into it. Nowanswer[i]is (left product) times (right product).
Why it is correct
At the start of iteration i in the left pass, the running value equals values[0] * ... * values[i-1] (the empty product 1 when i = 0). Likewise in the right pass it equals values[i+1] * ... * values[n-1]. Because each value is stored or applied before the current element is multiplied in, the element at i is never included. Zeros need no special case: a zero simply appears in every product that spans it, which yields the correct all-zero or single-nonzero patterns automatically.
Complexity
- Time:
O(n), two linear passes. - Space:
O(1)extra beyond the output list, since only one running-product variable is used at a time.
Reference solution
from typing import List
def leave_one_out_products(values: List[int]) -> List[int]:
n = len(values)
answer = [1] * n
# Left pass: answer[i] = product of values[0..i-1]
running = 1
for i in range(n):
answer[i] = running
running *= values[i]
# Right pass: multiply by product of values[i+1..n-1]
running = 1
for i in range(n - 1, -1, -1):
answer[i] *= running
running *= values[i]
return answer