Intermediate
Data Structures & Algorithms
Q71 / 100
Why is a hash map preferred over an array for the "two sum" problem when aiming for O(n) time?
Correct! Well done.
Incorrect.
The correct answer is A) A hash map allows O(1) average lookup of a complement value while scanning once, avoiding the O(n²) nested-loop comparison
A
Correct Answer
A hash map allows O(1) average lookup of a complement value while scanning once, avoiding the O(n²) nested-loop comparison
Explanation
By storing each visited number's index in a hash map, you can check in O(1) average time whether the complement (target minus current value) has already been seen, reducing the brute-force O(n²) approach to a single O(n) pass.
Progress
71/100