Data Structures & AlgorithmsHard

What is the length of the longest contiguous subarray of 4 5 6 5 4 7 8 9 10 3 in which every ADJACENT pair differs by exactly 1 (in either direction)?

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

Correct answer: D. 5

Scan the adjacent differences and track the longest stretch where each step is exactly ±1; the run length is one more than the number of such steps. Counting the adjacent pairs, or allowing a difference of at most one, changes the answer.

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

Exams that ask this topic