Skip to content

Product of Array Except Self

MediumArrays & Hashing
Decode it first. Clues, constraints, pattern and plan before any code.

Given a list of integers nums, return a list answer where answer[i] is the product of every number except nums[i]. Do it in O(n) time without using division.

nums = [1, 2, 3, 4]     →  [24, 12, 8, 6]
nums = [2, 3]           →  [3, 2]
nums = [1, 2, 0, 4]     →  [0, 0, 8, 0]

Constraints

  • 2 <= nums.length <= 10^5
  • -30 <= nums[i] <= 30
  • Every prefix and suffix product fits in a 32-bit integer.