DEV Community 👩‍💻👨‍💻

Discussion on: Daily Coding Problem #1

sanderintveld profile image
Sander in 't Veld

If we sort the list first, I can do you one better: have two iterators/indices, i at the start and j at the end. If the sum of the pointees is k, return true. If it is less, increment i. If it is more, decrement j. If i and j meet (and the sum did not equal k), return false. This is O(n).

Unfortunately sorting the list is O(n log(n)).

aukat profile image
(Carl) Esben Poulsen

Good point. I forgot that sorting is expensive