Two Sum – Pair sum in sorted array

Two Integer Sum II – Medium Level Problem

You are given a sorted array of integers called numbers, arranged in non-decreasing order. Your task is to find two numbers in the array that add up to a given value called target.

You need to return the positions of these two numbers as a list, [index1, index2], using 1-based indexing (the first element of the array is at position 1). The following conditions must be met:

  1. The two indices, index1 and index2, must be different.
  2. The value at index1 should appear before the value at index2 in the array.

There is exactly one correct solution for this problem. Your solution should use constant extra space (O(1)).

Two Sum Integer

Explanation: The sum of 1 and 2 is 3. Since we are assuming a 1-indexed array, index1 = 1, index2 = 2. We return [1, 2].

Constraints:

  • 2 <= numbers.length <= 1000
  • -1000 <= numbers[i] <= 1000
  • -1000 <= target <= 1000

Two Integer Sum II Solution

Recommendation for Time and Space Complexity – Your goal is to find a solution that works in O(n) time and uses O(1) extra space, where n is the length of the input array.

Hints for solving problems

Hint 1 :

A brute force solution would be to check every pair of numbers in the array. This would be an O(n^2) solution. Can you think of a better way?

 

Hint 2 :

Can you think of an algorithm by taking the advantage of array being sorted?

Hint 3 :

We can use the two-pointer algorithm. If nums[0] + nums[n-1] > target, then we know nums[n – 1] can not possibly be included in any pairs. Why? Because nums[n – 1] is the largest element in the array. Even by adding it with nums[0], which is the smallest element, we still exceed the target. You can think of the case when nums[0] + nums[n – 1] < target.

Hint 4 :

We keep two pointers, one at the start and the other at the end of the array. If the sum of the numbers at the two pointers is greater than the target, decrement the right pointer, else increment the left pointer. Repeat this process until you find a valid pair.

There are mainly 4 approach to solve this problem-

  1. Brute Force Method
  2. Binary Search Method
  3. Hash Map Method
  4. Two Pointers Method

1. Brute Force Method

Iterate through each pair of numbers in the array to check if their sum equals the target. This method has a time complexity of O(n^2).

  • Time complexity: O(n^2)
  • Space complexity: O(1)

Code

2. Binary Search Method

For each number in the array, use binary search to find the complement (target – current number). This approach works efficiently on a sorted array and has a time complexity of O(nlogn).

  • Time complexity: O(n^2)
  • Space complexity: O(1)

Code

3. Hash Map Method

Use a hash map to store numbers and their indices while traversing the array. Check if the complement of the current number exists in the hash map. This method has a time complexity of O(n).

  • Time complexity: O(n)
  • Space complexity: O(n)

Code

4. Two Pointers Method

Use two pointers—one starting at the beginning and the other at the end of the sorted array. Adjust the pointers based on the sum of the elements they point to, achieving a time complexity of O(n).

  • Time complexity: O(n)
  • Space complexity: O(1)

Code

More Articles