Two Sum II - Input Array Is Sorted - Leetcode Solution


đź’ˇ Step-by-Step Thought Process

  1. Understand the problem: Find two numbers in a sorted array that sum to a target and return their 1-based indices.
  2. Get the length n of the input array numbers.
  3. Initialize two pointers: l to 0 (start) and r to n-1 (end).
  4. While l is less than r:
  5. Compute the sum of numbers[l] and numbers[r].
  6. If the sum equals the target, return a list with l+1 and r+1 (1-based indices).
  7. If the sum is less than the target, increment l to try a larger number.
  8. If the sum is greater than the target, decrement r to try a smaller number.

Code Solution


                

                

                

                

Detailed Explanation

Understanding the Problem: Two Sum II - Input Array Is Sorted

The “Two Sum II – Input Array Is Sorted” problem asks you to find two numbers in a sorted array that add up to a specific target and return their 1-based indices. The key insight is that the array is already sorted, which enables a much more efficient solution than brute-force searching.

Example:

  • Input: numbers = [2, 7, 11, 15], target = 9
  • Output: [1, 2] (because 2 + 7 = 9)

Why This Problem Matters

This problem is a classic use case for the two-pointer technique, optimized for sorted arrays. It emphasizes the importance of leveraging input constraints (sortedness) to improve performance, a key strategy in competitive programming and software engineering.

Brute Force vs. Optimized Approach

A brute-force method would check every pair of elements to find a match — which works but is inefficient at O(n²) time complexity. Thankfully, since the input array is sorted, we can solve it in linear time using a two-pointer approach.

Optimal Solution: Two Pointers

The two-pointer technique involves scanning the array from both ends toward the center. At each step, you check the sum of the elements pointed to by the left and right pointers.

Steps:

  1. Initialize two pointers:
    • left = 0 (start of array)
    • right = numbers.length - 1 (end of array)
  2. While left < right:
    • Calculate sum = numbers[left] + numbers[right]
    • If sum == target, return [left + 1, right + 1] (1-based indexing)
    • If sum < target, increment left to increase the sum
    • If sum > target, decrement right to decrease the sum

Example Walkthrough

Input: numbers = [2, 3, 4], target = 6

  • left = 0, right = 2 → 2 + 4 = 6
  • Match found → return [1, 3]

Time and Space Complexity

Time Complexity: O(n), where n is the number of elements. Each element is visited at most once.
Space Complexity: O(1), since the solution uses only two pointers and no extra data structures.

Edge Cases to Consider

  • Exactly two elements in array → only one pair to check
  • No valid pair (not possible per problem guarantees)
  • Multiple valid pairs (only the first one encountered is returned)
  • Negative numbers and zero handled correctly with same logic

Conclusion

The “Two Sum II” problem is an elegant demonstration of the power of two-pointer strategies. By leveraging the sorted nature of the input, we eliminate the need for nested loops and achieve linear performance with constant space usage. It's a textbook case for writing clean, efficient, and optimal code using minimal memory.

Get Personalized Support at AlgoMap Bootcamp đź’ˇ