4. Product of Array Except Self
Medium · Array
Given an array of integers, return a new array where each element at index i is the product of all the other elements in the original array (excluding the element at index i itself).
Input: An array of integers nums.
Output: Return an array of the same length where answer[i] equals the product of all elements in nums except nums[i].
For example, if nums = [1,2,3,4], then answer[0] = 2×3×4 = 24, answer[1] = 1×3×4 = 12, and so on.
Examples
Example 1 Input: [1, 2, 3, 4] Output: [24, 12, 8, 6] Explanation: product of all except each index
Example 2 Input: [-1, 1, 0, -3, 3] Output: [0, 0, 9, 0, 0] Explanation: zero makes most positions zero
Constraints
- Standard input/output constraints apply