Data Structures & AlgorithmsHard

What is the length of the longest CONTIGUOUS subarray of 1 3 5 6 8 10 11 12 that is an arithmetic progression (all consecutive differences equal)?

  1. A. 9
  2. B. 8
  3. C. 4
  4. D. 28

Correct answer: B. 8

A contiguous arithmetic run has constant difference, so scan the differences and track the longest stretch of equal consecutive differences, adding one for the first element. Overshooting by one, looking at a prefix only, or counting subarrays gives different values.

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

Exams that ask this topic