1

I have an array like this

[2, 3, 4, 5]

I want to find two numbers whose multiplication result is 6

11
  • Is array sorted ? Can it have negative numbers ? Commented May 3, 2020 at 5:41
  • 2
    Can you show us what algorithm you have tried? Commented May 3, 2020 at 5:41
  • The array is not sorted and has only positive numbers. Commented May 3, 2020 at 6:02
  • Will the array ever have numbers greater than the multiplication result? Commented May 3, 2020 at 6:05
  • 1
    I want to exit after finding a given pair Commented May 3, 2020 at 6:27

5 Answers 5

2

This prints all pairs of numbers in the list whose product equals 6.

const list = [2, 3, 4, 5];

for (var i = 0; i < list.length; i++) {
  for (var j = i+1; j < list.length; j++) {
    if (list[i] * list[j] == 6) {
      console.log(list[i] + ' * ' + list[j] + ' = 6');
    }
  }
}

As pointed in a comment, there are more efficient solutions. Here is one of them, with the drawback that it is a little more complicated:

const list = [2, 3, 4, 5]
const res = 6;

const elems = new Set()
list.forEach(num => {
  if (num == 0 && res == 0) {
    console.log('0 times anything is 0');
  } else if (res%num == 0 && elems.has(res/num)) {
    console.log(num + ' * ' + res/num + ' = ' + res);
  } else {
    elems.add(num);
  }
});

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

Comments

1

Using ES 2019.

var array = [1, 2, 3, 4, 5, 6]
var result = 6

array.flatMap(
    (v, i) => array.slice(i + 1).map( w => v * w === result && console.log(v, w))
);

Comments

1

You can increase performance significantly, using the given solution. First, eliminate unused data then perform find algorithm.

const data = [2, 3, 4, 5];
const find = (data, result) => {
  for (let k = 1; k < data.length; k = k + 1) {
    for (let i = 0, j = data.length - 1; i < j; i++, j--) {
      if (data[i] * data[j] === result) {
        return [data[i], data[j]];
      }
    }
    let temp = data[data.length - 1];
    data[data.length - 1] = data[k];
    data[k] = temp;
  }
};
const findClean = (data, result) => {
  return find(
    data.filter((num) => result % num === 0),
    result
  );
};
console.log("find:" + find(data, 6));
console.log("find:" + find(data, 8));
console.log("find:" + find(data, 10));
console.log("find:" + find(data, 15));

console.log("findClean:" + findClean(data, 6));
console.log("findClean:" + findClean(data, 8));
console.log("findClean:" + findClean(data, 10));
console.log("findClean:" + findClean(data, 15));

2 Comments

i think looks fine...but may want to filter the array if it contains any elements greater than multiplication result. I am not sure if his array will be only integers or not, but that could change the constraints even further.
added.. both solution.
0

like as

 function foo(arr, res) {
let numbers = []
for (let i = 0; i < arr.length; i ++) {
  for (let k = i + 1; k < arr.length; k ++) {
    if (arr[i] * arr[k] === res) {
      return [...numbers, arr[i], arr[k]]
    }
  }
}
  return([...numbers])
  }

console.log(foo([2, 3, 4, 5], 6))

Comments

0
let result = []
const list = [2, 3, 4, 5];
list.some(n => {
  if (6 % n === 0 && list.includes(6/n)) {
    result = [n, 6/n];
    return true;
  }
})

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.