Given an array arr[] of n integers, construct a Product Array prod[] (of same size) such that prod[i] is equal to the product of all the elements of arr[] except arr[i]. Example : arr[] = {1,2,3,4,5} prod[] = {120, 60, 40, 30, 24 }

This one would be done using calculating the product of all numbers in an array and then dividing the current element value. Since it could also be done using log and exp without using any divide operation.

var product = input => {
let sum = input.reduce((acc, cval) => {
return acc + Math.log(cval);
}, 0);
let result = input.map(x => {
return Math.round(Math.exp(sum - Math.log(x)));
});
console.log(result);
};
product([1, 2, 3, 4, 5]);

If you know other way of solving this problem of O(n). Please discuss in comments section.

Popular posts from this blog

Features of URL Shortener Extension.

Update for Chrome URL Shortener extension.