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?
- A. 5
- B. 2
- C. 3
- 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.
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 windows of length 3 in 1 3 2 4 6 5 7 9 are strictly increasing?
- How many contiguous subarrays of length at least 2 of 5 1 2 1 6 2 have a MAXIMUM element strictly greater than the sum of all their other…
- How many contiguous subarrays of 2 3 1 4 2 2 have a sum EQUAL to their maximum element?
- How many index triples i < j < k in 1 2 3 4 5 6 7 satisfy A[i] + A[j] = A[k]?
- How many contiguous subarrays of 1 2 3 1 2 1 4 have the sum of their elements at EVEN relative positions equal to the sum at ODD relative…
- How many contiguous subarrays of 2 5 1 5 3 4 contain the maximum VALUE of the subarray exactly once?