Two Product Problem

Published by Mubashir Hassan in

Create a function that takes an array arr and a number n and returns an array of two integers from arr whose product equals n.

Examples

twoProduct([1, 2, -1, 4, 5], 20) ➞ [4, 5]

twoProduct([1, 2, 3, 4, 5], 10) ➞ [2, 5]

twoProduct([100, 12, 4, 1, 2], 15) ➞ []

Note:

  • Try doing this with 0(N) time complexity.
  • No duplicates.
  • In the array, there can be multiple solutions so return the solution with the lowest sum of indexes of product pairs (i.e. N = 10, solutions = [[2, 5], [10, 1]], indexes = [[600, 3000], [800, 900]], return [10, 1]).
  • The array can have multiple solutions that share the lowest sum of indexes, so return the first full match that's found (as described above) that also has the lowest sum of indexes.
Watch a quick demo on how Edabit works.