Pair With Given Sum (Sorted Array)

Given a sorted array of integers and a target value, return true if there exists a pair of distinct elements whose sum equals the target. This uses the two-pointer approach to find a pair in O(n) time.

Input Format

nums = array of integers, target = required sum

Output Format

true if a valid pair exists, false otherwise

Constraints

  • 1 <= nums.length <= 10^5; -10^6 <= nums[i], target <= 10^6

Examples

Example 1:

Input:

nums = [1,2,3,4]
target = 5

Output:

true

Explanation:

The pair (1, 4) sums to 5.

Loading...
Pair With Given Sum (Sorted Array) - Arrays