Skip to content

Commit cfd8940

Browse files
committed
ProductExceptSelf Dec 4
1 parent 11bf512 commit cfd8940

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Arrays/productOfArrayExceptSelf.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
var productExceptSelf = function(nums) {
3+
4+
const prefixArray=[];
5+
6+
const suffixArray=[];
7+
8+
let prefixProduct=1;
9+
let suffixProduct=1;
10+
11+
for(let ind=0;ind<nums.length;ind++){
12+
13+
if(ind==0){
14+
prefixArray[0]=prefixProduct;
15+
}
16+
else{
17+
prefixProduct*=nums[ind-1];
18+
prefixArray[ind]=prefixProduct;
19+
}
20+
}
21+
for(let ind=nums.length-1;ind>=0;ind--){
22+
23+
if(ind==nums.length-1){
24+
suffixArray[ind]=1;
25+
}
26+
else{
27+
suffixProduct*=nums[ind+1];
28+
suffixArray[ind]=suffixProduct;
29+
}
30+
}
31+
let result=[];
32+
33+
for(let j=0;j<prefixArray.length;j++){
34+
35+
result[j]=(prefixArray[j]*suffixArray[j]);
36+
}
37+
38+
return result;
39+
};

0 commit comments

Comments
 (0)