Given an integer array nums, return an array output where output[i] is the product of all the elements of nums except nums[i].
Each product is guaranteed to fit in a 32-bit integer.
Follow-up: Could you solve it in time without using the division operation?
Example 1:
Input: nums = [1,2,4,6]
Output: [48,24,12,8]Example 2:
Input: nums = [-1,0,1,2,3]
Output: [0,-6,0,0,0]Constraints:
2 <= nums.length <= 1000-20 <= nums[i] <= 20
You should aim for a solution as good or better than O(n) time and O(n) space, where n is the size of the input array.
A brute-force solution would be to iterate through the array with index i and compute the product of the array except for that index element. This would be an O(n^2) solution. Can you think of a better way?
Is there a way to avoid the repeated work? Maybe we can store the results of the repeated work in an array.
We can use the prefix and suffix technique. First, we iterate from left to right and store the prefix products for each index in a prefix array, excluding the current index's number. Then, we iterate from right to left and store the suffix products for each index in a suffix array, also excluding the current index's number. Can you figure out the solution from here?
We can use the stored prefix and suffix products to compute the result array by iterating through the array and simply multiplying the prefix and suffix products at each index.
For each position in the array, we can compute the product of all other elements by multiplying every value except the one at the current index.
This directly follows the problem statement and is the most straightforward approach:
for each index, multiply all elements except itself.
Although simple, this method is inefficient because it repeats a full pass through the array for every element.
n be the length of the input array and create a result array res of size n.i from 0 to n - 1:prod = 1.j from 0 to n - 1:j is not equal to i, multiply prod by nums[j].prod in res[i].res.class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
n = len(nums)
res = [0] * n
for i in range(n):
prod = 1
for j in range(n):
if i == j:
continue
prod *= nums[j]
res[i] = prod
return resThis approach works by using a simple idea:
If we know the product of all non-zero numbers, we can easily compute the answer for each position using division — as long as there are no division-by-zero issues.
So we first check how many zeros the array has:
n.0.total_product // nums[i].class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
prod, zero_cnt = 1, 0
for num in nums:
if num:
prod *= num
else:
zero_cnt += 1
if zero_cnt > 1: return [0] * len(nums)
res = [0] * len(nums)
for i, c in enumerate(nums):
if zero_cnt: res[i] = 0 if c else prod
else: res[i] = prod // c
return resFor each index, we need the product of all elements before it and all elements after it.
Instead of recomputing the product repeatedly, we can pre-compute two helpful arrays:
pref[i] = product of all elements to the left of isuff[i] = product of all elements to the right of iThen, the final answer for each index is simply:
result[i] = prefix[i] × suffix[i]
This works because:
Both pieces together form the product of all numbers except the one at that position.
Let n be the length of the array.
Create three arrays of size n:
pref for prefix productssuff for suffix productsres for the final resultSet:
pref[0] = 1 (nothing to the left of index 0)suff[n - 1] = 1 (nothing to the right of last index)Build the prefix product array:
i from 1 to n - 1:pref[i] = nums[i - 1] × pref[i - 1]Build the suffix product array:
i from n - 2 down to 0:suff[i] = nums[i + 1] × suff[i + 1]Build the result:
i, compute:res[i] = pref[i] × suff[i]Return the result array.
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
n = len(nums)
res = [0] * n
pref = [0] * n
suff = [0] * n
pref[0] = suff[n - 1] = 1
for i in range(1, n):
pref[i] = nums[i - 1] * pref[i - 1]
for i in range(n - 2, -1, -1):
suff[i] = nums[i + 1] * suff[i + 1]
for i in range(n):
res[i] = pref[i] * suff[i]
return resWe can compute the product of all elements except the current one without using extra prefix and suffix arrays.
Instead, we reuse the result array and build the answer in two simple passes:
res[i] with the product of all elements to the left of i (prefix product).res[i] with the product of all elements to the right of i (postfix product).By maintaining two running values — prefix and postfix — we avoid the need for separate prefix and suffix arrays.
This gives us the same logic as the previous method, but with O(1) extra space.
res with all values set to 1.prefix = 1.i:res[i] = prefix (product of all elements to the left).prefix *= nums[i].postfix = 1.i:res[i] by postfix (product of all elements to the right).postfix *= nums[i].res.class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
res = [1] * (len(nums))
prefix = 1
for i in range(len(nums)):
res[i] = prefix
prefix *= nums[i]
postfix = 1
for i in range(len(nums) - 1, -1, -1):
res[i] *= postfix
postfix *= nums[i]
return res