Product of Array Except Self
Medium
Arrays
Prefix Sum
Given an integer array `nums`, return an array where each element is the product of all elements except itself. Do NOT use division.
Input: space-separated integers
Output: space-separated products
Constraints
2 <= nums.length <= 100000
-30 <= nums[i] <= 30
Product fits in 32-bit integer.
Examples
Example 1:
Input: 1 2 3 4
Output:
Example 2:
Input: -1 1 0 -3 3
Output:
Hints
Use prefix products from left.
Then multiply by suffix products from right.
Tests:
Runtime:
Memory:
Test
Input:
Expected:
Got:
Click Run Code to test against sample cases, or Submit to test against all cases.
▲ Console