Pairs with Sum

Pairs with Sum

Find all pairs within an array which sum to a specified value.
Algorithm:
  1. Sort array in ascending order
  2. Set leftIndex = 0 and rightIndex = 0
  3. Iterate till leftIndex < rightIndex
    1. Set localSum = array[leftIndex] + array[rightIndex];
    2. If localSum == sum then
      1. Print both indices and increase leftIndex and decrease rightIndex by 1
    3. Else If localSum < sum then
      1. increase leftIndex by 1
    4. Else
      1. decrease rightIndex by 1

Latest Source Code:
Github: PairsWithSum.java


Output:

Pair: (-1, 14)
Pair: (0, 13)
Pair: (6, 7)
Author: Hrishikesh Mishra