Two Product Problem (Part 2)

Published by persolut in

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

Examples

TwoProduct(new int[] { 1, 2, 3, 4, 13, 5 }, 39) ➞  { 3, 13 }

TwoProduct(new int[] { 11, 2, 7, 3, 5, 0 }, 55) ➞ { 5, 11 }

TwoProduct(new int[] { 100, 12, 4, 1, 2 }, 15) ➞ { }

Notes

  • No duplicates.
  • Sort the result.
  • Try doing this with O(n) time complexity.
  • The array can have multiple solutions, so return the first match you find.
  • If there is no solution return an empty array (3rd example).
Watch a quick demo on how Edabit works.