Product of Array Except Self
Given an integer array nums, return an array answer such that answer[i] is equal to the product of all elements of nums except nums[i]. The product of any…
Given an integer array `nums`, return an array `answer` such that `answer[i]` is equal to the product of all elements of `nums` except `nums[i]`. The product of any prefix or suffix of `nums`, and every value in `answer`, is guaranteed to fit in a **32-bit integer**. You must write an algorithm that runs in `O(n)` time and **without using the division operation**. Use `O(1)` auxiliary space, excluding the required output array.
- 2 <= nums.length <= 10^5 - -30 <= nums[i] <= 30 - The product of any prefix or suffix of nums, and every answer value, is guaranteed to fit in a 32-bit integer. - The interactive visualizer accepts at most 12 values; longer valid arrays are execution-only.
Example 1: input nums = [1,2,3,4]; output [24,12,8,6]. answer[0] = 2×3×4 = 24, answer[1] = 1×3×4 = 12, answer[2] = 1×2×4 = 8, answer[3] = 1×2×3 = 6.
Example 2: input nums = [10,3,5,6,2]; output [180,600,360,300,900]. answer[0] = 3×5×6×2 = 180, answer[1] = 10×5×6×2 = 600, etc.
Example 3: input nums = [1,0,3,4]; output [0,12,0,0]. Only the position of the zero gets a non-zero result; all others include the zero in their product.
Category: Arrays. Topic: arrays. Difficulty: Medium. Patterns: prefix-sum.