Product of Array Except Self

Given an integer array nums, return an array answer of the same length where answer[i] is the product of every element of nums except nums[i].

You may not use division, and the algorithm should run in O(n) time. Try to use only O(1) extra space besides the output array.

Constraints

  • 2 <= nums.length <= 10^5
  • -30 <= nums[i] <= 30
  • The product of any prefix or suffix of nums fits in a 32-bit integer.

Examples

Example 1

Input: nums = [2,3,4,5]
Output: [60,40,30,24]

answer[0] = 3 × 4 × 5 = 60, answer[1] = 2 × 4 × 5 = 40, answer[2] = 2 × 3 × 5 = 30, answer[3] = 2 × 3 × 4 = 24.

Example 2

Input: nums = [3,0,2]
Output: [0,6,0]

Every product except the one that skips the 0 includes the 0. Skipping index 1 gives 3 × 2 = 6.

Example 3

Input: nums = [0,0,4]
Output: [0,0,0]

Two zeros: every product includes at least one of them.

JavaScript you'll use: Arrays: create, read, update

/**
 * @param {number[]} nums
 * @return {number[]}
 */
function productExceptSelf(nums) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.