Data Structures & AlgorithmsHardTCS NQT · Accenture

How many values x in [2, 4, 6, 7, 8] have both x - 1 and x + 1 present in the array?

  1. A. 2
  2. B. 1
  3. C. 3
  4. D. 5

Correct answer: B. 1

Only 7 has both 6 and 8 present. A hash set answers each membership test in O(1).

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic