SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Course Overview
Algorithm Patterns OverviewFrequency CountersTwo SumValid AnagramGroup AnagramsFrequency Counters and Hash-Map LookupTwo PointersValid PalindromeTwo Sum IIContainer With Most WaterTwo-Pointer ReasoningSliding WindowMaximum Average Subarray ILongest Substring Without Repeating CharactersMinimum Size Subarray SumSliding-Window Reasoning
Algorithm Patterns

Two Sum II

Apply opposite-direction pointers to find a target pair in a sorted array.

Try It Yourself

Find the target pair in the sorted array. JavaScript and Python start array indices at 0. The answer counts positions starting at 1, so add 1 to each index when you return it.

Use constant extra space

Find the pair with two pointers and O(1) extra space without building a lookup map. Use the array's sorted order to decide which pointer should move after each sum. Handle negative and duplicate values.

Run the examples before submitting.

Two Sum II — Input Array Is Sorted

mediumTwo Pointers · Arrays25 minLC 167
Problem

Given an array of integers sorted in non-decreasing order and a target, find two elements at distinct indices whose sum equals the target. Their values may be equal.

Return their positions as [index1, index2] in ascending order, counting the first position as 1.

Example 1:

Input: numbers = [2, 7, 11, 15], target = 9
Output: [1, 2]
Explanation: 2 + 7 = 9. The code reads these values at indices 0 and 1. Counting from 1, they occupy positions 1 and 2, so return [1, 2].

Example 2:

Input: numbers = [2, 3, 4], target = 6
Output: [1, 3]
Explanation: 2 + 4 = 6. These are the first and third elements, so return [1, 3].

Constraints

  • 2 ≤ numbers.length ≤ 30,000
  • Exactly one solution exists.
  • Use O(1) extra space.
0 attempts

Test results

Results appear here after a run.

Solution

For target 13, 1 + 15 is too large, 1 + 11 is too small, and 2 + 11 matches.

Start left at the first element and right at the last. If the sum is too small, even the largest remaining partner falls short for numbers[left], so move left one step right. If the sum is too large, even the smallest remaining partner makes the sum too large for numbers[right], so move right one step left.

In the figure, the values 2 and 11 match the target at zero-based indices 1 and 4. They are the second and fifth elements, so [left + 1, right + 1] gives [2, 5].

function twoSumSorted(numbers, target) {
  let left = 0;
  let right = numbers.length - 1;

  while (left < right) {
    const sum = numbers[left] + numbers[right];

    if (sum === target) return [left + 1, right + 1];
    if (sum < target) left++;
    else right--;
  }

  return [];
}

Big O notation

MeasureComplexityExplanation
TimeO(n)Each check either returns the answer or moves one pointer inward. There are at most n - 1 checks.
Auxiliary spaceO(1)Only two pointer indices and the current sum are stored.

How is this practice?

Previous lessonValid PalindromeNext lessonContainer With Most Water
Edit on GitHubReport on GitHub
On this page
%

On this page

Try It YourselfSolutionBig O notation