Two Sum on an unsorted array, return indices. A candidate proposes left and right pointers moving inward. What is wrong?
Show answer
Answer
Pointer moves are only valid on sorted input, and sorting loses the original indices
Moving left up when the sum is too small relies on sorted order. Without sorting the moves skip valid pairs, and sorting needs index tracking. It is not O(n2), it is wrong. A visited set of pointers fixes nothing.
Source: LeetCode 1. Two Sum