1

Problem:

Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i.

For example:

  • if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24].

  • If our input was [3, 2, 1], the expected output would be [2, 3, 6].

Solution 1 (With Nested loops): I'm able to solve this by nested loops like below:

const input = [1, 2, 3, 4, 5];

function output(items) {
const finalArray = [];
 for (let i = 0; i < items.length; i++) {
    let multipliedNum = 1;
    
    items.forEach((item, indx) => {
      if (i !== indx) {
        multipliedNum = multipliedNum * item;
      }
    });
         finalArray.push(multipliedNum)

    }
  return finalArray;

}

console.log(output(input))

I'm trying to find out another solution without nested loops inside output function? Any help or suggestion really appreciated.

3
  • What's the range of values in the array? Can a value be zero? Commented Dec 22, 2021 at 17:52
  • 4
    math time: "the product of all elements, excepting the one at i" is the same as "the product of all elements, divided by the element at i". So you just need two iterations without nesting: the first to calculate the product, and then the second to generate all the requested values. Commented Dec 22, 2021 at 17:52
  • @GenericUser Let suppose there is no zero in the array for now. Commented Dec 22, 2021 at 17:53

4 Answers 4

2

If there are no zero values, you can loop through all the values once to get the product. Then just return the array where each the product is divided by each entry.

However, if there are zeros then there is a little more to be done to check how many there are. One zero is fine but more than 1 means that the value is zero for each entry.

const input = [1, 2, 3, 4, 5];
const input2 = [1, 2, 3, 4, 0];
const input3 = [1, 2, 3, 0, 0];

function output(items) {
  let zeroCount = 0;
  let totalProduct = 1;
  for (let i = 0; i < items.length; i++) {
    if (items[i] === 0) {
      if (++zeroCount > 1) break;
      continue;
    }
    totalProduct *= items[i];
  }

  if (zeroCount > 1) {
    // more than 1 zero -> all values are 0
    return new Array(items.length).fill(0);
  } else if (zeroCount === 1) {
    // only 1 zero -> only the value that is zero will be the totalProduct
    return items.map(item => item === 0 ? totalProduct : 0);
  }
  // no zero in array -> divide the totalProduct by each item
  return items.map(item => totalProduct / item);
}

console.log(output(input))
console.log(output(input2))
console.log(output(input3))

Sign up to request clarification or add additional context in comments.

Comments

1

Based on what @Mike said in the comment here's the answer.

const input = [1, 2, 3, 4, 5];
const mulValues = input.reduce((acc, next) => acc * next);
const output = input.map(i => mulValues/i)
console.log(output)

Comments

0

you can do something like that (assuming array doesn't contain zero):

  • calculate product of all array elements
  • divide product by element at position [i] to get the desired output
const input = [1, 2, 3, 4, 5];

function output(items) {
const finalArray = [];
const multipliedNum=1;
 for (let i = 0; i < items.length; i++) {
     multipliedNum *= item[i];
    }
 for (let i = 0; i < items.length; i++) {
      finalArray.push(multipliedNum/item[i]);
   }
  return finalArray;
}

console.log(output(input))

Comments

-1

I know this has already been answered, but I think I have a better one.

If you take this issue by a different approach you will see that the product leaving the value at the index out, is also the product devided by value at the index.

If you know use the reduce function, you can simply calculate the product in one line using:

items.reduce((a, b) => a * b)

and then just divide by the value you want to ignore... like this:

items.reduce((a, b) => a * b) / items[index]

if you now want to compress this in one line instead of wrapping it into a for loop block you can simply copy the array and use the map function and the result could look like this:

result = [...items].map((v, i) => items.reduce((a, b) => a * b) / v)

I hope that this helps you to reduce your code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.