Two Pointers pattern

Two Sum II

Given a 1-indexed array of integers sorted in non-decreasing order, return the 1-based indices of the two numbers that add up to a given target, so the first element is index 1, not 0. Exactly one solution exists, and you may not use the same element twice. Watch it run step by step, then write your own pseudocode and get it checked by AI.

  • LeetCode 167
  • Medium
  • 2 approaches

Sign in with Google or GitHub and you land straight on this page. DSA comes with the all labs plan and every ChaiCode batch.

Two PointersLeetCode 167

Medium

From the first idea to the best one

  1. 01 Brute force O(n²)O(1) space
  2. 02 Optimized · two pointers O(n)O(1) space
Each approach plays step by step, with its time and space.

What you will learn

  • How to read Two Sum II and spot the two pointers pattern in it
  • Brute force: O(n²) time, O(1) space
  • Optimized · two pointers: O(n) time, O(1) space
  • The pseudocode for each approach, its line lit as the animation runs
  • Practice: write your own pseudocode and get it checked

More Two Pointers

See Two Sum II run, step by step

Sign in and this page opens on the animation, with all 12 Two Pointers problems beside it.

DSA comes with the all labs plan and every ChaiCode batch.