The tempting shortcut is to compute the total product once and divide it by nums[i] for each answer โ but division is explicitly disallowed, and it would break on a zero anyway. The actual insight: answer[i] is the product of everything before i times everything after i. Those two pieces can each be built with a simple running pass.
Brute Force
Time O(nยฒ)Space O(1)For each index, multiply every other element.
class Solution: def productExceptSelf(self, nums: list[int]) -> list[int]: n = len(nums) result = [] for i in range(n): product = 1 for j in range(n): if j != i: product *= nums[j] result.append(product) return resultCorrect, but for each of the n output positions it redoes an O(n) multiplication pass โ O(nยฒ) total, recomputing overlapping products from scratch every time.
Prefix ร Suffix Products
OptimalTime O(n)Space O(1)answer[i] is (product of nums[0..i-1]) * (product of nums[i+1..n-1]). Build the prefix products left-to-right directly into the answer array, then sweep right-to-left multiplying in the suffix products with a single running variable โ no division, and no second array needed.
class Solution: def productExceptSelf(self, nums: list[int]) -> list[int]: n = len(nums) answer = [1] * n
prefix = 1 for i in range(n): answer[i] = prefix prefix *= nums[i]
suffix = 1 for i in range(n - 1, -1, -1): answer[i] *= suffix suffix *= nums[i]
return answerTracing nums = [1, 2, 3, 4] โ first the left-to-right prefix pass (before the multiply, answer[i] is set to the running prefix product of everything to its left):
answer[0] = prefix (1, product of nothing to the left). Update prefix to 1*1 = 1.
Then the right-to-left suffix pass multiplies each slot by everything to its right:
answer[3] *= suffix (1, nothing to the right yet) -> 6, unchanged. Update suffix to 1*4 = 4.
Correctness: after the prefix pass, answer[i] holds the product of everything strictly to the left of i. Multiplying in the suffix passโs running product โ everything strictly to the right of i โ gives exactly the product of all elements except nums[i], with no division and no risk of dividing by a zero.
Complexity: two linear passes over the array โ O(n) time. Only two scalar running variables (prefix, suffix) are used beyond the output array, which the problem explicitly excludes from the space count โ O(1) extra space, answering the follow-up.