← Arrays & Hashing Quiz: all 8 questions
Arrays & Hashing Quiz · Question 3 of 8
Why do we store x in the map only AFTER checking for target − x in Two Sum?
Why do we store x in the map only AFTER checking for target − x in Two Sum?
- A.To keep the map sorted
- B.So an element is never paired with itself
- C.Because maps cannot store duplicates
- D.To reduce memory usage
Answer & explanation
Correct answer: So an element is never paired with itself
If x = target / 2 and we stored it first, the lookup would find x itself and return the same index twice.
Can you get the rest right without peeking?
Take the timed Arrays & Hashing Quiz quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →