DEV Community

Saranya R
Saranya R

Posted on

Leetcode Two sum problem

1. Two Sum

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.

167. Two Sum II - Input Array Is Sorted

Working:

  • Start with two pointers left at the beginning, 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.

Top comments (0)