Skip to main content

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 the elements of nums except nums[i].

The product of any prefix or suffix of nums 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.

Input: nums = [1,2,3,4]

Output: [24,12,8,6]

/**
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelf = function(nums) {
const n = nums.length;

const leftProducts = new Array(n).fill(1);
const rightProducts = new Array(n).fill(1);

let leftProduct = 1;

for (let i = 1; i < n; i++) {
leftProduct *= nums[i - 1];
leftProducts[i] = leftProduct;
}

let rightProduct = 1;
for (let i = n - 2; i >= 0; i--) {
rightProduct *= nums[i + 1];
rightProducts[i] = rightProduct;
}

const result = [];
for (let i = 0; i < n; i++) {
result[i] = leftProducts[i] * rightProducts[i];
}

return result;
};