Finding desired numbers in a sorted array in JavaScript

We have an array of integers which is sorted in the increasing order. We are required to write a JavaScript function that takes in one such array as the first argument and a target sum number as the second argument.

The function should find and return two such numbers from the array that when added gives the target sum. The condition for solving this problem is that we have to do this in linear time and using constant space.

Two Pointer Approach

The optimal solution uses the two-pointer technique. Since the array is sorted, we can place one pointer at the beginning and another at the end, then move them based on the sum comparison.

Example

Following is the code ?

const arr = [4, 6, 8, 9, 11, 12, 18, 21];
const num = 27;

const findElements = (arr = [], target) => {
   let left = 0;
   let right = arr.length - 1;
   let res = [];
   
   while (left < right) {
      let leftElement = arr[left];
      let rightElement = arr[right];
      
      if (leftElement + rightElement === target) {
         res.push(arr[left]);
         res.push(arr[right]);
         break;
      } else if (leftElement + rightElement > target) {
         right--;
      } else {
         left++;
      }
   }
   return res;
};

console.log(findElements(arr, num));

Output

Following is the console output ?

[6, 21]

How It Works

The algorithm works by maintaining two pointers:

  • Left pointer: Starts at index 0 (smallest element)
  • Right pointer: Starts at the last index (largest element)

At each step, we calculate the sum of elements at both pointers:

  • If sum equals target: we found our pair
  • If sum is greater than target: move right pointer left (decrease sum)
  • If sum is less than target: move left pointer right (increase sum)

Complete Example with Edge Cases

const findTwoSum = (arr = [], target) => {
   if (arr.length < 2) return [];
   
   let left = 0;
   let right = arr.length - 1;
   
   while (left < right) {
      const sum = arr[left] + arr[right];
      
      if (sum === target) {
         return [arr[left], arr[right]];
      } else if (sum > target) {
         right--;
      } else {
         left++;
      }
   }
   
   return []; // No pair found
};

// Test cases
console.log(findTwoSum([1, 2, 3, 4, 6], 6));  // [2, 4]
console.log(findTwoSum([2, 7, 11, 15], 9));   // [2, 7]
console.log(findTwoSum([1, 2, 3], 7));        // [] (no pair)
console.log(findTwoSum([3], 6));              // [] (single element)
[2, 4]
[2, 7]
[]
[]

Time and Space Complexity

  • Time Complexity: O(n) - each element is visited at most once
  • Space Complexity: O(1) - only using two pointers and result array

Conclusion

The two-pointer approach efficiently finds pairs that sum to a target in sorted arrays. It leverages the sorted property to achieve linear time complexity while using constant extra space.

Updated on: 2026-03-15T23:19:00+05:30

253 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements