Product of Array Except Self
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].
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
You must solve the problem without using the division operator and in O(n) time complexity.
Example 1
Input
4 1 2 3 4
Output
24 12 8 6
Explanation
The product except self for each index is:
Index 0: 2 × 3 × 4 = 24
Index 1: 1 × 3 × 4 = 12
Index 2: 1 × 2 × 4 = 8
Index 3: 1 × 2 × 3 = 6
Example 2
Input
5 -1 1 0 -3 3
Output
0 0 9 0 0
Explanation
Compute the product of all elements except the current one without using division.
The resulting array is 0 0 9 0 0.
Constraints
Hints:
Hint 1
For each index, compute the product of all elements to its left and all elements to its right.
Hint 2
Build the answer using prefix products in one pass and suffix products in another pass, achieving O(n) time and O(1) extra space (excluding the output array).
Author & Technical Reviewer
Technically reviewed by: ExamAdda Technical Review Team
Technical Reviewers, ExamAdda
Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.
Published
Aug 4, 2026
Last updated
Aug 4, 2026
Content Verification Methodology
Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.
Expected Output