Meta / Amazon / Google / Microsoft| Product of Array Except Self | LeetCode-238: Medium | JavaScript Implementation

Manan Kumar Gupta
1 min readMar 15, 2022

We will be seeing the solution to the LeetCode problem 238 in JavaScript. I hope you will find it useful.

Problem Statement:
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.

Link to Problem: https://leetcode.com/problems/product-of-array-except-self/

JavaScript Implementation:

var productExceptSelf = function(nums) {
let prefix = [1]
let subfix = []
subfix[nums.length-1]=1
for(let i = 1;i<nums.length;i++){
prefix[i] = prefix[i-1] * nums[i-1]
}
for(let i = nums.length-2;i>=0;i--){
subfix[i]=subfix[i+1] * nums[i+1]
}

let answer = []
for(let i= 0;i<nums.length;i++){
answer.push(subfix[i]*prefix[i])
}
return answer
};

Thank you for reading hope this code is helpful to you.

--

--

Manan Kumar Gupta

I am Frontend Developer, exploring the power of Javascript. I have worked on Angular, ReactJS and React Native.