Product Except Self With Zeros
Problem statement
You are given an integer array nums. Return an array result where result[i] is the product of all elements in nums except nums[i].
Handle three zero cases:
- If there are no zeroes, each position receives the product of all numbers divided by the current value.
- If there is exactly one zero, only the zero position receives the product of all non-zero values.
- If there is more than one zero, every output value is
0.
Function
productExceptSelf(nums: int[]) → int[]Examples
Example 1
nums = [1,2,0,4]return = [0,0,8,0]Only the index holding the zero gets a nonzero product; every other product includes the zero.
Constraints
- The input contains integers.
- In the no-zero case, integer division gives exact results.