Closest Sum Pair

Two Pointers Arrays Algorithm

Problem Description

Given a sorted array of integers and a target integer, your task is to find two numbers in the array whose sum is closest to the target value. Return the pair of numbers as a list (or array) of two elements. If there are multiple pairs that yield the same closest sum, you may return any one of them.

Constraints:

  • The array will contain at least two integers.
  • The array is sorted in non-decreasing order.
  • The solution should aim for O(n) time complexity using the two-pointer technique.

Example:

Input: nums = [1, 3, 4, 7, 10], target = 15

Output: [4, 10]

Explanation: The sum of 4 and 10 is 14, which is the closest sum to the target 15. Note that another valid answer might be [7, 7] if the list had duplicate values summing to a closer number, but given the input this pair is unique.

Happy New Year! Start the year by cracking some coding challenges.

Starter Code

Below is some language-agnostic starter code for you to begin your solution: