Data Structures & AlgorithmsModerateGATE CS · Codeforces Div2 B

What is the length of the longest run of consecutive integers (in any order) present in the array [9, 1, 4, 7, 3, -1, 0, 5, 8, -1, 6]?

  1. A. 6
  2. B. 8
  3. C. 7
  4. D. 10

Correct answer: C. 7

Put all values in a hash set and start counting only from values whose predecessor is absent, so each run is walked once. Duplicates such as the two -1 values must not inflate the count.

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

Exams that ask this topic