Chaturmind
LearnDSASystem DesignDevOpsEngineering GrowthBlog
Start learning
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML
  • DevOps
  • Engineering Growth

Company

  • Blog
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.

← Arrays & Strings Mastery

Core Patterns

  • Two Pointers Pattern
  • Sliding Window Pattern
  • Prefix Sums
  • Practice problems

    Two Sum
  • Maximum Subarray
  • Product of Array Except Self
  • Best Time to Buy and Sell Stock
  • Container With Most Water
  • 3Sum
  • Subarray Sum Equals K
  • Running Sum of 1D Array

String Problems

  • String Hashing & Anagrams
  • String Two-Pointer Problems
  • Practice problems

    Valid Anagram
  • Group Anagrams
  • Valid Palindrome

Two Pointers & Sliding Window

  • Practice problems

    Two Sum II — Input Array Is Sorted
  • Remove Duplicates from Sorted Array
  • Maximum Sum Subarray of Size K
  • First Negative Integer in Every Window of Size K
  • Longest Substring Without Repeating Characters
  • Minimum Window Substring
Chaturmind
← Arrays & Strings Mastery

Core Patterns

  • Two Pointers Pattern
  • Sliding Window Pattern
  • Prefix Sums
  • Practice problems

    Two Sum
  • Maximum Subarray
  • Product of Array Except Self
  • Best Time to Buy and Sell Stock
  • Container With Most Water
  • 3Sum
  • Subarray Sum Equals K
  • Running Sum of 1D Array

String Problems

  • String Hashing & Anagrams
  • String Two-Pointer Problems
  • Practice problems

    Valid Anagram
  • Group Anagrams
  • Valid Palindrome

Two Pointers & Sliding Window

  • Practice problems

    Two Sum II — Input Array Is Sorted
  • Remove Duplicates from Sorted Array
  • Maximum Sum Subarray of Size K
  • First Negative Integer in Every Window of Size K
  • Longest Substring Without Repeating Characters
  • Minimum Window Substring
HomeLearnArrays & Strings MasteryTwo Pointers & Sliding Window
EasyTwo Pointers & Sliding Window

Two Sum II — Input Array Is Sorted

two-pointerssorted-arrayarray

Problem

Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target. Return the indices of the two numbers, 1-indexed, as an array of length 2.

Examples

Example 1

Input: numbers = [2,7,11,15], target = 9

Output: [1,2]

Explanation: numbers[0] + numbers[1] = 2 + 7 = 9, returned 1-indexed.

Example 2

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

Output: [1,3]

Explanation: numbers[0] + numbers[2] = 2 + 4 = 6.

Constraints

  • •2 <= numbers.length <= 3 * 10^4
  • •-1000 <= numbers[i] <= 1000
  • •numbers is sorted in non-decreasing order
  • •Exactly one valid answer exists

Hints

Hint 1

The array being sorted is the whole point — it means you don't need a hash map like unsorted Two Sum.

Hint 2

Start pointers at both ends. What does it tell you if the current sum is too big? Too small?

Hint 3

Because it's sorted, moving the left pointer only ever increases the sum, and moving the right pointer only ever decreases it — that monotonicity is what makes two pointers correct here.

Solutions

public int[] twoSumHashMap(int[] numbers, int target) {
    Map<Integer, Integer> seen = new HashMap<>();
    for (int i = 0; i < numbers.length; i++) {
        int complement = target - numbers[i];
        if (seen.containsKey(complement)) {
            return new int[]{seen.get(complement) + 1, i + 1};
        }
        seen.put(numbers[i], i);
    }
    throw new IllegalArgumentException("No solution");
}

Time: O(n) · Space: O(n)

Previous · Practice problem

Valid Palindrome

Next · Practice problem

Remove Duplicates from Sorted Array