mediumArrayArrays & Hashing

Product of Array Except Self

mediumTime: O(n)Space: O(1)

Signals to notice

product of all elements except currentno division allowedprefix and suffix

Brute force first

For each element, multiply all others. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.

The key insight

Two passes: left-to-right prefix products, then right-to-left suffix products. Instead of recomputing the world every time, you preserve just enough context to let the next decision become obvious.

What must stay true

result[i] = product of all elements to the left × product of all elements to the right. As long as that statement keeps holding, you can trust the steps built on top of it.

Easy way to go wrong

Trying to use division — the problem explicitly forbids it, and zeros break division anyway. Most mistakes here are not about syntax; they come from losing track of what your state, pointer, or structure is supposed to mean.

Arrays & Hashing Pattern