Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

241
Views
How to multiply array values without nested loops

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.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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))

about 4 years ago · Santiago Trujillo Report

0

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)
about 4 years ago · Santiago Trujillo Report

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))
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!