Skip to content
AI360Xpert
Beta
Difficulty: MediumArrays & Hashing

Product of Array Except Self

Problem in Plain English

For each position, multiply every number except the one at that position. Return those products in order, without division, in linear time.

Problem Statement

Given an integer array nums, build an array answer of the same length. For each index i, answer[i] must multiply all values in nums except nums[i]. Exclude a position, not every occurrence of its value.

The solution must take O(n)O(n) time without division, where n is the array length. Products of all prefixes (sections starting at the first element) and suffixes (sections ending at the last element) fit in a signed 32-bit integer.

Follow-up: use O(1)O(1) extra space. The returned answer array does not count as extra space; any other arrays do.

Constraints

  • 2 <= nums.length <= 100000
  • -30 <= nums[i] <= 30
  • Every answer[i], prefix product, and suffix product fits in a signed 32-bit integer.

Examples

Example 1

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

Skip each position: 2 * 3 * 4 = 24, 1 * 3 * 4 = 12, 1 * 2 * 4 = 8, and 1 * 2 * 3 = 6.

Example 2

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

At the smallest allowed length, each answer is simply the other number.

Example 3

Input
nums = [-1, 1, 0, -3, 3]
Output
[0, 0, 9, 0, 0]

Only index 2 skips the zero: (-1) * 1 * (-3) * 3 = 9; every other product contains zero.

Example 4

Input
nums = [0, 2, 0, 4]
Output
[0, 0, 0, 0]

Skipping one position still leaves at least one zero in every product.

Intuition

Split the array at the position you must skip: everything to its left and everything to its right. Their products combine into the answer. For index 2 in [1, 2, 3, 4], the left product is 1 * 2 = 2, the right product is 4, and the answer is 2 * 4 = 8. A side with no elements contributes 1, because multiplying by 1 leaves the other side unchanged. Two sweeps reuse these products instead of starting over for each position.

Approaches

Brute Force

Solution Details

Reveal Brute Force: intuition, complexity, and code

Hints

Hint 1
At one chosen position, what remains on the left and on the right after you skip it?
Hint 2
A left-to-right pass can keep the product of everything already visited. Save that product before including the current value.
Hint 3
Use the output array for left products, then walk backward with a running right product. Combine the two sides before extending the right product.

Edge Cases

  • Smallest valid input: [2, 5] returns [5, 2]. Empty and single-element arrays are outside this problem's constraints.
  • Duplicates: [2, 2, 2] returns [4, 4, 4]. Skip one position, not every matching value.
  • All ones: [1, 1, 1] returns [1, 1, 1]; starting products at 1 also handles an empty side.
  • One zero: [-1, 1, 0, -3, 3] returns [0, 0, 9, 0, 0]. Only the zero's position can have a nonzero answer.
  • Two zeros: [0, 2, 0, 4] returns all zeros. Even a long run of large values between two zeros must return zeros, never NaN.
  • Negatives: [-1, 2, -3, 4] returns [-24, 12, -8, 6]; ordinary multiplication preserves the correct signs.
  • Numeric limits: Java int and JavaScript Number handle the guaranteed prefix, suffix, and answer bounds. Without those bounds, reassess overflow and integer precision.

Common Mistakes and Interview Tips

  • Using total-product division: division is forbidden, and zeros make that shortcut invalid.
  • Extending prefix or suffix before updating answer: this wrongly includes the current element. State the rule: the running product excludes the current index.
  • Starting a running product at zero, or using addition: the empty product is 1, and the operation is multiplication.
  • Interview tip: explain the quadratic baseline, then the two-pass rule. Say explicitly that O(1) is extra space excluding the output; separate prefix and suffix arrays would use O(n) extra space.

Key Takeaway

In the Arrays & Hashing pattern, two directional sweeps can combine information from both sides of an index. Save one side in the output and carry the other in a running value to avoid repeated work and extra arrays.