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)?
- A. 9
- B. 8
- C. 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- How many contiguous subarrays of 1 4 2 6 3 5 0 have their FIRST element equal to their minimum AND their LAST element equal to their…
- How many contiguous subarrays of 3 1 6 2 9 4 5 12 contain EXACTLY 2 multiples of 3?
- What is the length of the longest contiguous subarray of 4 6 3 8 10 12 5 2 4 in which EVERY element is even and whose length is odd?
- How many contiguous subarrays of 1 3 2 4 5 3 1 6 have a sum divisible by 4 AND an odd length?
- How many index pairs i < j in 2 3 4 6 8 12 9 satisfy A[i]·A[j] = 24?
- A min-stack stores a pair (value, current minimum) per entry. Why does popping never require recomputing the minimum?