Two Pointers

Concept Lab
Python Execution SynchronizerActive Line: 1
1
def two_sum_sorted(numbers, target):
2
    left = 0
3
    right = len(numbers) - 1
4
    
5
    while left < right:
6
        current_sum = numbers[left] + numbers[right]
7
        if current_sum == target:
8
            return [left + 1, right + 1]
9
        elif current_sum < target:
10
            left += 1
11
        else:
12
            right -= 1
13
            
14
    return []
No execution event recorded.
Custom Input:
Loading trace state...
Step 0 / 0