Data Structures & AlgorithmsVery hardCodeforces Div2 B-D · ICPC regional
What is the length of the longest contiguous subarray of [1, 2, 1, 2, 3, 2, 2, 4, 1, 1, 1] that contains at most 2 distinct values?
- A. 4
- B. 7
- C. 3
- D. 6
Correct answer: A. 4
Shrink the window from the left whenever the distinct count exceeds k. Allowing 3 distinct values (k = 3) is the trap for those who forget the limit.
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
- For the positive array [2, 3, 1, 2, 4, 3] and target S = 7, what is the length of the shortest contiguous subarray whose sum is at least 7?
- Five stalls are at positions 1, 2, 4, 8, 9 on a line. Three cows are placed in distinct stalls so that the minimum distance between any…
- A conveyor must ship packages with weights 1, 2, 3, ..., 10 in the given order, in at most 5 days, and loads cannot be reordered. What is…
- What is the sum over all subarrays of [3, 1, 2, 4] of the minimum element of each subarray?
- Using a monotonic deque, the window maximum for every window of size 3 over [1, 3, -1, -3, 5, 3, 6, 7] is listed left to right. What is…
- In the array [1, 2, 1, 3, 2, 5] exactly two values appear once and every other value appears twice. Which two values are they, as a sorted…