Product of Array Except Self
Build left and right product arrays without using division.
Problem Statement
Given an integer array nums, return an array answer such that answer[i] equals the product of all elements of nums except nums[i]. You must write an algorithm that runs in O(n) time and without using the division operation.
Examples
Example 1
Explanation: answer[0]=2×3×4=24, answer[1]=1×3×4=12...
Example 2
Constraints
- 2 ≤ nums.length ≤ 10⁵
- -30 ≤ nums[i] ≤ 30
- No division allowed.
Solutions
Build a left[i] array where left[i] = product of all elements before i. Build a right[i] where right[i] = product of all elements after i. Answer[i] = left[i] * right[i].
Related Concepts
Deepen your understanding with these related topics from our AI Glossary:
Want to master the core concepts?
Our free AI Glossary covers 190+ topics — from Prefix Sum to Dynamic Programming, Machine Learning, SQL, and more. Structured learning tracks for every level.
Stuck? Ask AI to explain it step by step.
Ask Claude, GPT-4o, or Gemini to debug your code, generate test cases, or walk through the intuition. 39+ models. Pay only on days you use it — no subscription required.
Free to start · No credit card required to explore
Get Started Free