5. Product of Array Except Self
For each position, multiply all values except the value at that position. Build the complete result without using division, and leave the original array unchanged.
Zeros are ordinary input values and must be handled correctly. The result concerns positions, so another equal-valued element is still included in the product.
Example 1
Input
[1,2,3,4]
Output
[24,12,8,6]
Explanation: Each entry combines factors strictly to its left and right.
Example 2
Input
[-1,1,0,-3,3]
Output
[0,0,9,0,0]
Explanation: Only the zero’s position excludes the unique zero; its remaining product is 9.
Example 3
Input
[0,0,2]
Output
[0,0,0]
Explanation: Every excluded-position product still contains a zero.
Constraints
0 <= nums.length <= 105-231 <= nums[i] <= 231 - 1- The product of the magnitudes of all nonzero values is at most 2^63 - 1. Return a new array of exact products.
Hints
Hint 1
Separate the factors before i from the factors after i.
Hint 2
Store the left product without including nums[i].
Hint 3
Multiply those stored prefixes by a running exclusive suffix in a backward pass.
Submissions
Your recent submissions for this problem.
Checking sign-in…
Discussion
Ask a question or share an approach. Posts are public; avoid personal information and mark solution spoilers clearly.
No discussion yet. Start the conversation with a question about this problem.
Checking sign-in…
Run your code to check the sample cases, or submit it to check the full validation suite.
Two exclusive directional products
[1,2,3,4]
[24,12,8,6]