Array of Products

Nerly Ton
3 min readMay 14, 2021

Algo — Medium

The following will be a quick walkthrough on a brute force solution for the array of products problem.

Problem:

Write a function that takes in an array of integers and returns an array of the same length, where each element in the output array is equal to the product of every other number in the input array.

Example:

array = [5, 1, 4, 2]
output = [8, 40, 10, 20]

explanation:
8 = 1*4*2
40 = 5*4*2
10 = 5*1*2
20 = 5*1*4

Solution:

Below is the brute force solution using division. For us to get the output we need to return the total of all the numbers being multiplied and then divide by the value that the number is on based on index.
For example: Looking at the explanation above. 5*1*4*2 = 40
40 / 5 = 8
40/1 = 40
40/4 = 10
40/2 = 20

Now it's easier said than done when it comes to the code since you want to catch the edge cases; for example, if there is a zero anywhere in the array or if the whole array is zeros.

In the beginning, we can declare some variables.
-newArray to be an empty array to push the results into it
-num = 1 in order to multiple the numbers by 1 and not interfere with the total
-counter = 0 to keep track of all the zeros in the array (whether it is one or more) It's important you will see why later
-rememberNum = to keep track of the index of the zero if there is only one

After we will check If the array is empty, we can automatically just return the array and finish the problem.

We have two separate loops. In the first loop, we will check to see if there are any zeros and counter accordingly.

  • If the counter > 1, we can return an array of the same length as the original array and fill it with zeros.
    example: [1,0,2,5,0]
    if we cover the 1 = 0*2*5*0 = 0
    if we cover the 0 = 1*2*5*0 = 0
    etc..
    as you can see since we only ever cover one index then there will always be another zero which will always bring the total to 0.
  • if the counter =1, then we remember the index of that one zero and and return a new array of the same length filled with zero’s except on the index of where the original zero was located because in that case it means the the total does not zero out since we covered the zero and multiplied all the other numbers.
    example: [1,0,2,5]
    if we cover the 1 = 0*2*5 = 0
    if we cover the 0 = 1*2*5 = 10
    if we cover the 2 = 1*0*5 = 0
    if we cover the 5 = 1*0*2 = 0
    Therefore the array will be filled with zero EXCEPT on the index in which the zero was.

Now if there are no zeros at all then we can resume as normal and multiple them all and then divide by the value of the index and push to the empty array.

function arrayOfProducts(array) {
let newArray = []
let num = 1
let counter = 0
let rememberNum
if(array.length === 0) {
return array
}

for(let i = 0; i < array.length; i++) {
if(array[i] === 0) {
counter += 1
rememberNum = i
if(counter > 1) {

return new Array(array.length).fill(0);
}
}
num *= array[i] == 0 ? 1 : array[i]
}

for(let i = 0; i < array.length; i++) {
if (i == rememberNum){
newArray = new Array(array.length).fill(0);
newArray[i] = num
return newArray
}
let n = num/array[i]
newArray.push(n)
}
return newArray
}

Hope this walk-through gave some clarity to the problem. Being this is the brute force, we need to make it better by not solving it with division involved. I'm excited to tackle the challenge.

--

--