VOOZH about

URL: https://www.geeksforgeeks.org/dsa/given-an-absolute-sorted-array-and-a-number-k-find-the-pair-whose-sum-is-k/

⇱ Given an absolute sorted array and a number K, find the pair whose sum is K - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Given an absolute sorted array and a number K, find the pair whose sum is K

Last Updated : 12 Jul, 2025

Given an absolute sorted array arr[] and a number target, the task is to find a pair of elements in the given array that sum to target. If no such pair exist in array the return an empty array.

An absolute sorted array is an array of numbers in which |arr[i]| <= |arr[j]|, for all i < j.

Examples:

Input: arr[] = [-8, 10, 12, -15, 15, 24], target =  0
Output: [-15, 15]
Explanation: The sum of elements at index 3 and 4 i.e., -15 and 15 is equal to target.

Input: arr[] = [-8, 10, 12, -15, 15, 24], target =  -23
Output: [-8, -15]
Explanation: The sum of elements at index 0 and 3 i.e., -8 and -15 is equal to target.

Input: arr[] = [-8, 10, 12, -15, 15, 24], target =  20
Output: [ ]
Explanation: No pair exist whose sum is equal to target.

[Naive Approach] Using Nested Loop - O(n ^ 2) Time and O(1) Space

The simple approach is to check all possible pairs and identify the pair that adds up to the target. We can use nested loop, where the outer loop selects the first element and the inner loop selects the second element.


Output
-15 15 

[Better Approach] Using Hash Set - O(n) Time and O(n) Space

The idea is that for every element x in the array arr[], we can check if its complement (target - x), exists in the array. To make this search faster, we can use a hash set.


Output
-15 15 

[Expected Approach] Using Two Pointers - O(n) Time and O(1) Space

For a sorted array, use the approach discussed in this article. In case of an absolute sorted array, there are generally three cases for pairs according to their property: 

  1. Both the numbers in the pair are negative.
  2. Both the numbers in the pair are positive.
  3. One number is negative and the other is positive.

For cases (1) and (2), use the Two Pointer Approach separately by just limiting to consider either positive or negative numbers. 
For case (3), use the same two pointer approach where we have one index for positive numbers and one index for negative numbers, and they both start from the leftmost possible index and then move towards right, skipping there respective opposite numbers.

Note: In absolute sorted array, the negative numbers were arranged in descending order.

Case 1: Both the numbers in the pair are positive, initialize i = 0, j = size - 1.

  • Skip if either arr[i] or arr[j] is negative.
  • If arr[i] + arr[j] < target, to increase the sum, increment i.
  • If arr[i] + arr[j] > target, to decrease the sum, decrement j.
  • If arr[i] + arr[j] == target, pair found.

Case 2: Both the numbers in the pair are negative, initialize i = 0, j = size - 1.

  • Skip if either arr[i] or arr[j] is positive.
  • If arr[i] + arr[j] < target, to increase the sum, decrement j.
  • If arr[i] + arr[j] > target, to decrease the sum increment i.
  • If arr[i] + arr[j] == target, pair found.

Case 3: One number is negative and the other is positive., initialize i = 0, j = 0.

  • Skip if arr[i] is negative or arr[j] is positive.
  • If arr[i] + arr[j] < target, to increase the sum, increment i.
  • If arr[i] + arr[j] > target, to decrease the sum, increment j.
  • If arr[i] + arr[j] == target, pair found.

Output
-15 15 
Comment
Article Tags: