Leetcode Two sum problem

Published: (March 26, 2026 at 12:36 PM EDT)
1 min read
Source: Dev.to

Source: Dev.to

Two Sum

Two Sum illustration

Working

  • seen keeps track of numbers we’ve already visited and their indices.
  • For each num, we calculate complement = target - num.
  • If the complement is already in seen, we return the pair of indices.
  • Otherwise, we store the current number with its index.

Two Sum II – Input Array Is Sorted

Two Sum II illustration

Working

  • Start with two pointers left at the beginning and right at the end.
  • Compute the sum of the two numbers.
  • If the sum equals the target, return their indices.
  • If the sum is too small, move left forward to increase the sum.
  • If the sum is too large, move right backward to decrease the sum.
0 views
Back to Blog

Related posts

Read more »

Leetcode Reflection 3.16-3.22

Weekly Reflection LeetCode > “I realize it’s too clumsy to post for every LeetCode problem I solved, so I’ll accumulate a week’s reflections together and post...