Data Structures & AlgorithmsVery hard

What is the length of the longest contiguous subarray of 3 4 4 5 3 2 6 6 7 in which every two elements differ by at most 1, i.e. max − min ≤ 1?

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

Correct answer: C. 3

A window whose values all lie within two adjacent integers can be tracked with a count of each value, shrinking when a third value appears. Allowing spread 2, or requiring all equal values, changes the maximal window.

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

Exams that ask this topic