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

Working
seenkeeps track of numbers we’ve already visited and their indices.- For each
num, we calculatecomplement = 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

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
leftforward to increase the sum. - If the sum is too large, move
rightbackward to decrease the sum.