Data Structures & AlgorithmsVery hard

Which property lets the 'longest subarray where every adjacent pair differs by exactly 1' be found in one pass?

  1. A. The condition depends on the global maximum only.
  2. B. The answer is always the array length.
  3. C. The condition is local: it holds for a block exactly when every adjacent pair satisfies it, so a run of consecutive good pairs can be tracked with one counter.
  4. D. Values must be sorted for the condition to be checked.

Correct answer: C. The condition is local: it holds for a block exactly when every adjacent pair satisfies it, so a run of consecutive good pairs can be tracked with one counter.

Because the condition is about adjacent pairs, a maximal run of good pairs gives the longest qualifying block, and a single counter tracks its length. Restarting at each broken pair handles the rest.

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

Exams that ask this topic