An efficient solution uses the two-pointer technique, which takes advantage of the fact that the array is already sorted.

  1. Initialize Pointers: Start with two pointers. left points to the beginning of the array (index 0), and right points to the end.

    left, right = 0, len(numbers) - 1
  2. Loop and Check Sum: Loop as long as the left pointer is less than the right pointer. In each iteration, calculate the sum of the values at the left and right pointers.

    while left < right:
        current_sum = numbers[left] + numbers[right]
  3. Adjust Pointers Based on Sum:

    if current_sum > target:
        right -= 1
    elif current_sum < target:
        left += 1
    else:
        # Solution found, return indices
  4. Return Result: Once the sum matches the target, return the indices of the two numbers. Since the problem asks for 1-based indexing, add 1 to both left and right before returning.

    return [left + 1, right + 1]

This two-pointer algorithm is much more efficient, with a time complexity of O(n) and a space complexity of O(1) as it only requires a single pass through the array.

Final Python Code

class Solution:
  def twoSum(self, numbers: list[int], target: int) -> list[int]:
    left, right = 0, len(numbers) - 1

    while left < right:
      current_sum = numbers[left] + numbers[right]

      if current_sum > target:
        right -= 1
      elif current_sum < target:
        left += 1
      else:
        return [left + 1, right + 1]