Skip to main content

Two Sum II - Input Array Is Sorted

Return 1-indexed positions of two numbers summing to target in a sorted array

Two Sum II - Input Array Is Sorted rewards a clean two-pointer invariant instead of checking every combination. Interviewers love it because it reveals your invariant-driven scanning and your edge-case discipline. Keep the invariant in your head and the pointer moves become obvious.

Examples

You write

two_sum(numbers: List[int], target: int)

  1. Example 1

    Input
    numbers = [2,7,11,15], target = 9
    returns
    [1,2]
  2. Example 2

    Input
    numbers = [2,3,4], target = 6
    returns
    [1,3]

The pattern behind it

Arrays & Hashing

I reach for it when

  • You catch yourself scanning the array again for every element.
  • The problem asks about duplicates, pairs, counts or groups.

Say this before you type. Say what the key is and what the value remembers, before you write the loop.

All 127 Arrays & Hashing problems

Two Pointers

I reach for it when

  • The array is sorted, or you can sort it.
  • You need a pair, a triple or a palindrome.

Say this before you type. Say what each pointer means and what a move to the left or right rules out.

All 26 Two Pointers problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Two Sum IImedium
2Part 2
3Part 3

Each locked part opens in the editor once the part before it passes.