Data Structures & AlgorithmsHardCodeforces Div2 C · GATE CS
How many contiguous subarrays of [1, 2, 1, 3, 2, 2, 1] contain at most two distinct values?
- A. 17
- B. 15
- C. 19
- D. 16
Correct answer: D. 16
Count windows with at most two distinct values using a sliding window that counts, for each right end, the valid left ends. Only the subarrays containing 3 are excluded when they have a third value.
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
- You may increase any element of [2, 5, 3, 9] by 1 per operation, and you want all elements equal. What is the minimum number of operations?
- In the binary row [1, 1, 0, 1, 1, 1] treated as a histogram of height 1, what is the largest rectangle area?
- What is the length of the shortest contiguous subarray of [5, 1, 4, 3, 2, 6, 7] whose sum is at least 15?
- In [6, 3, 6, 9, 3, 9, 9, 5, 5] exactly one value appears an odd number of times and every other value appears an even number of times…
- How many index pairs i < j in [12, 21, 30, 43, 66, 111, 3] have equal decimal digit sums?
- How many ordered index pairs (i, j) in [1, 3, 4, 7, 10, 12, 15] satisfy a[j] - a[i] = 3?