2Brute2Best
🔥0

Product of Array Except Self

Medium·LeetCode #238 Pro OnlyMedium
LeetCode
01

Problem Statement

You are given an integer array nums.

Build an array answer where answer[i] is the product of every number in nums except nums[i].

Rules: you may not use division, and your solution must run in O(N) time. It is guaranteed that every prefix product and suffix product fits in a 32-bit integer.

Follow-up: can you do it with O(1) extra space? The output array does not count as extra space.

Input

An integer array nums of length n.

Output

An array of n integers: answer[i] = product of all nums[j] with j ≠ i.

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

answer[0] = 2·3·4 = 24, answer[1] = 1·3·4 = 12, answer[2] = 1·2·4 = 8, answer[3] = 1·2·3 = 6.

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

Every product that includes the 0 is 0. Only index 2 skips it: (−1)·1·(−3)·3 = 9.

Constraints
  • • 2 ≤ n ≤ 10^5
  • • −30 ≤ nums[i] ≤ 30
  • • Every prefix and suffix product fits in a 32-bit integer.
  • • No division allowed.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“product of every number except nums[i]”
→ Except i = everything before i and everything after i → a left pass and a right pass.
“without division”
→ You can't take the total and remove nums[i]. You must build the two sides separately.
“O(1) extra space, output doesn't count”
→ Store one pass in the output array and carry the other in a variable.
03

Approach Complexity Ladder

Brute Force — multiply everything else for each indexO(N²)O(1)

For each index, loop over the array and multiply all other numbers

Better — prefix-product and suffix-product arraysO(N)O(N)

Store the product before each index and after each index, then multiply

Optimal — prefix in the output, running suffixO(N)O(1)

Fill answer with prefix products, then sweep from the right with one suffix variable

Brute Force Intuition

For each i, start with 1 and multiply every nums[j] with j ≠ i. Store the result.

Recognition rule for this sub-pattern:

Each index needs information from its left and its right. Build a left pass and a right pass, then combine.
Remember this · Prefix Sum
Trigger
Range sums, "subarray sum equals K" (especially with negatives), equal counts, divisible by K.
Think
If I knew the total up to every index, would the answer become a difference of two totals?
Key question
Which earlier prefix value am I looking for when I stand at index i?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Prefix Sum translate into production architectures and low-level memory mechanics.