Algorithms and Data Structures · Middle · Question 19 of 75

Two Sum on a sorted array: what's the O(n) approach without extra memory?

Explanation

Sortedness tells you which pointer to move: increasing the left value raises the sum, decreasing the right value lowers it. A hash map also works in O(n) but needs O(n) memory.

Further reading

Problem with a question

Algorithms and Data Structures · Middle · question 19

Test yourself

Take the Algorithms and Data Structures · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz