Product of Array Except Self
Product of Array Except Self
Section titled “Product of Array Except Self”
Medium
Day 1 • Striver Blind 75
📌 Problem Overview
Section titled “📌 Problem Overview”Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
You must solve it without division and in O(n) time.
Examples & Constraints
Section titled “Examples & Constraints”Example 1:
- Input:
nums = [1,2,3,4] - Output:
[24,12,8,6]
Example 2:
- Input:
nums = [-1,1,0,-3,3] - Output:
[0,0,9,0,0]
Constraints:
2 ≤ nums.length ≤ 10⁵-30 ≤ nums[i] ≤ 30
💡 Approach & Intuition
Section titled “💡 Approach & Intuition”Product of Array Except Self tests prefix/suffix product techniques to avoid division while staying at O(n) time.
🎯 Pattern Recognition
Section titled “🎯 Pattern Recognition”Pattern: Prefix/Suffix Accumulation
When each result depends on everything except the current element, compute a running product from the left, then a running product from the right, and combine.
📊 Step-by-Step Execution (Mermaid Diagram)
Section titled “📊 Step-by-Step Execution (Mermaid Diagram)”graph TD Start["Input Data"] --> Process["Process Element by Element"] Process --> Lookup{"Hash Map / Set Lookup"} Lookup -- "Match Found" --> Return["Return Indices / Result"] Lookup -- "No Match" --> Store["Store in Map / Set"] Store --> Process🐢 Brute Force Solution
Section titled “🐢 Brute Force Solution”function productExceptSelf(nums) { return nums.map((_, i) => nums.reduce((p, n, j) => i === j ? p : p * n, 1));}- Time Complexity:
O(n²) - Space Complexity:
O(n) - Explanation: Recompute the product for every index.
⚡ Optimized Solution
Section titled “⚡ Optimized Solution”function productExceptSelf(nums) { const n = nums.length; const answer = new Array(n).fill(1); let prefix = 1; for (let i = 0; i < n; i++) { answer[i] = prefix; prefix *= nums[i]; } let suffix = 1; for (let i = n - 1; i >= 0; i--) { answer[i] *= suffix; suffix *= nums[i]; } return answer;}- Time Complexity:
O(n) - Space Complexity:
O(1) excluding output - Explanation: Two passes accumulating prefix and suffix products.
🐾 Step-by-Step Walkthrough
Section titled “🐾 Step-by-Step Walkthrough”- Initialize State: Setup necessary pointers, dynamic programming arrays, or hash maps.
- Iterate & Evaluate: Process the input according to the boundary conditions.
- Update & Return: Compute the optimal answer and return early or at termination.
🎙️ FAANG Interview Pitch
Section titled “🎙️ FAANG Interview Pitch”- Note division would be simplest but is disallowed (and breaks with zeros)
- Build prefix products left to right
- Build suffix products right to left, multiplying into the same array
- Discuss O(1) extra space excluding the output array
💡 Progressive Hints
Section titled “💡 Progressive Hints”- Compute the product of all elements to the left of each index.
- Compute the product of all elements to the right of each index.
- Multiply the two together for the final answer, without using division.