TwoSum


Algorithmic thinking Searching Public

Easy  

30min


Write a function that, given a vector and a target sum, returns zero-based indices of any two distinct elements whose sum is equal to the target sum. If there are no such elements, the function should return (-1, -1).

For example, findTwoSum({ 1, 3, 5, 7, 9 }, 12) should return any of the following pairs of indices:

  • 1, 4 (3 + 9 = 12)
  • 2, 3 (5 + 7 = 12)
  • 3, 2 (7 + 5 = 12)
  • 4, 1 (9 + 3 = 12)
   

  •   Example case: Exception
  •   No solution: Exception
  •   One solution: Exception
  •   Performance test with a large number of elements: Exception