← Back to Games

Prefix Product Quest

Product of array except self - using prefix and suffix products!

📦 Original Array
⬅️ Prefix Products (product of all elements to the left)
➡️ Suffix Products (product of all elements to the right)
✨ Result (prefix[i] × suffix[i])

🔍 Query: Product Except Self

Select an index to see the calculation

🧠 Problem: Product of Array Except Self

Given an array, return an array where each element is the product of all other elements except itself.

Constraint: Don't use division! (What if there's a zero?)

Step 1: Prefix Products

prefix[i] = product of arr[0..i-1]

prefix[0] = 1
prefix[i] = prefix[i-1] × arr[i-1]
Step 2: Suffix Products

suffix[i] = product of arr[i+1..n-1]

suffix[n-1] = 1
suffix[i] = suffix[i+1] × arr[i+1]
Step 3: Combine

result[i] = prefix[i] × suffix[i]

// No division needed!
// O(n) time, O(n) space