Data Structures & AlgorithmsVery hardCodeforces Div2 D · ICPC regional
How many contiguous subarrays of [4, 2, 3, 1, 6, 5, 7, 9] satisfy max − min = (length of the subarray) − 1? Count every such subarray, including those with repeated values.
- A. 16
- B. 18
- C. 15
- D. 8
Correct answer: A. 16
Repeated values can also satisfy max − min = length − 1 (for example [1, 3, 3]), so the test must be done on the max and min directly rather than by assuming distinct values. With sliding-window max and min deques, each window is tested in amortised O(1). Counting the increasing runs answers a different question.
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
- What is the length of the longest substring of "bbaacabaacb" that contains EXACTLY two letters 'a'?
- Choose exactly 4 elements of [6, 1, 9, 4, 7, 2, 8, 5] (by position) such that no two chosen positions are adjacent. What is the maximum…
- How many permutations of 1, 2, 3, 4, 5 have exactly 3 inversions (pairs of positions i < j with p[i] > p[j])?
- What is the minimum number of positive perfect cubes (1, 8, 27, ...) whose sum is exactly 31? Repeats are allowed.
- How many substrings of "abacaxayaba" are palindromes of ODD length at least 3?
- Among all distinct permutations of the letters of "dbca" sorted alphabetically, what is the 1-based position of "dbca" itself?