🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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?

  1. A.To keep the map sorted
  2. B.So an element is never paired with itself
  3. C.Because maps cannot store duplicates
  4. 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 →