Two Pointers/Medium

Two Sum II - Input Array Is SortedFREE TRACK

Time: O(N)Space: O(1)

Initialize Two Pointers

Array is sorted. Set left pointer L=0 (val 2) and right pointer R=3 (val 15).

Array Elements & Pointers
L
2
L
7
[1]
11
[2]
R
15
R
Live Variables & Invariants
target:9
left:0
right:3
sum:17
Step 1 / 714%
Solution Code
1
function twoSumII(numbers: number[], target: number): number[] {
2
  let left = 0, right = numbers.length - 1;
3
  while (left < right) {
4
    const sum = numbers[left] + numbers[right];
5
    if (sum === target) return [left + 1, right + 1];
6
    else if (sum < target) left++;
7
    else right--;
8
  }
9
  return [];
10
}

Custom Test Case Runner

Input your custom values and visualize step-by-step trace

Quick Presets:

AI DSA Coach

Contextual Tutor for Two Sum II - Input Array Is Sorted

Hello! I am your AI DSA Tutor for **Two Sum II - Input Array Is Sorted** (Two Pointers). Ask me anything about this algorithm, time complexity, or request a step hint!