Example – A = [2, 4, 9, 1, 10, 5] , Sum = 6

Output – (2,4) or (1,5) as 2+4 = 6 and 1+5 = 6

Approaches

  1. Naive – O(n^2) – Traverse through the array two times and check all other elements with each element of the array.
  2. Sorting – O(nlogn) – Sort the array. Maintain a low pointer at start and high pointer at end. Traverse and move pointers accordingly.
  3. Hashing – O(n) – As you traverse, map each element to dict/hashmap etc and check for the desired value in the map.

LEAVE A REPLY