Data Structures19 sections · 729 units
Open in Course

The Pair Finding Problem

Beyond sorted arrays

In the Arrays section, you solved two-sum on a sorted array using two pointers. But what if the array isn't sorted?

You could sort it first (O(nlog⁡n)O(n \log n)), but that changes indices. What if you need the original indices? Hash maps solve this in O(n)O(n) without sorting. The idea: for each element xx, check if target−xtarget - x exists.