#238
Product of Array Except Self
medium· Arrays & Hashingruns: 0Given an integer array nums, return an array answer such that answer[i] is the product of all the elements of nums except nums[i]. The algorithm must run in O(n) and must not use division.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 375
class Solution:
def productExceptSelf(self, nums: list[int]) -> list[int]:
n = len(nums)
result = [1] * n
prefix = 1
for i in range(n):
result[i] = prefix
prefix *= nums[i]
suffix = 1
for i in range(n - 1, -1, -1):
result[i] *= suffix
suffix *= nums[i]
return result
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.