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(nlogn)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 targetxtarget - x exists.