Skip to content
BASA DSA

Product of Array Except Self

MediumArrays & Hashing

For each index, return the product of every other element. Do not use division; aim for linear time and constant auxiliary space beyond the answer.

Example 1

Input
nums = [2,3,4]
Output
[12,8,6]

Explanation

Exclude each position: 34=12, 24=8, and 2*3=6.

Example 2

Input
nums = [0,5,0]
Output
[0,0,0]

Explanation

Each product still contains at least one of the two zeroes.

Example 3

Input
nums = [-2,0,3]
Output
[0,-6,0]

Explanation

Only excluding the zero leaves a nonzero product: -2*3=-6.

Constraints

  • 2 <= len(nums) <= 100000
  • Values are -30..30
  • Every prefix, suffix and answer product fits a signed 32-bit integer

Function contractproductExceptSelfArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…