205. Product of Array Except Self
Medium
Prefix and Suffix Products
Array
Problem
Given an integer array nums, return an array answer where answer[i] equals the product of every element of nums except nums[i]. Solve it without using division.
Examples
Example 1
Input: [1,2,3,4]
Output: [24,12,8,6]
Example 2
Input: [-1,1,0,-3,3]
Output: [0,0,9,0,0]
Example 3
Input: [2,3]
Output: [3,2]
Constraints
� 2 <= nums.length <= 100000
� -30 <= nums[i] <= 30
� Products fit within the integer range used by the judge.
� Division must not be used.
Hints
?? Compute the product of everything to the left of each index.
?? Then sweep from right to left while maintaining a suffix product.
Expected Complexity
Time: O(n)
Space: O(1) auxiliary
Follow-up
Can you achieve O(1) extra space excluding the output array?
Practice Notes
Attempts: 0
Time spent: 0min
Hints used: 0
JavaScript
00:00
Loading...
Case 1
Case 2
Case 3
Input
[1,2,3,4]Expected
[24,12,8,6]