Use a hash map from value to index to find the needed complement in O(1) average time.
Scan the array. For each number x, compute target - x. If the complement was seen before, return the pair of indices.
When you reach index i, every earlier value is already stored, so you can detect the unique valid pair as soon as the second element is processed.
Use a hash map from value to index to find the needed complement in O(1) average time.
Scan the array. For each number x, compute target - x. If the complement was seen before, return the pair of indices.
When you reach index i, every earlier value is already stored, so you can detect the unique valid pair as soon as the second element is processed.