Unique Pair Finder

Two Pointer Array Search

Problem Description

Given a sorted array of integers (which may include duplicates) and a target sum, write a function that returns all unique pairs of numbers whose sum equals the target. Each pair should be included only once. For example, even if the same two numbers appear multiple times in the array, they should contribute to a single pair in the result.

Use the two-pointer technique to achieve an optimal solution with O(n) time complexity.

Input

  • A sorted array of integers (e.g., [1, 1, 2, 2, 3, 4, 5]).
  • An integer target sum (e.g., 6).

Output

  • A list (or equivalent in your chosen language) of pairs, where each pair is a list/tuple of two integers. The order of pairs in the output does not matter.

Example

For the input array [1, 1, 2, 2, 3, 4, 5] and target 6, the expected output is:

[[1, 5], [2, 4]]

Constraints

  • You can assume that negative numbers may be present in the array.
  • If no valid pair exists, return an empty list.

Hints

  • Utilize a two-pointer approach: one pointer starting at the beginning and the other at the end of the array.
  • Make sure to skip duplicates to ensure that each pair is only counted once.

Happy coding and enjoy solving today's challenge on this fine February 5th!