Data Structures & AlgorithmsVery hardCodeforces Div2 D · LeetCode-style
How many contiguous subarrays of [1, 2, 1, 3, 3, 2, 4, 1] contain EXACTLY 2 distinct values?
- A. 9
- B. 18
- C. 12
- D. 10
Correct answer: A. 9
Use exactly(k) = atMost(k) − atMost(k − 1), where atMost is counted with a sliding window that tracks the number of distinct values. Counting windows with at most k distinct values 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
- Evaluate 3 + 5 2 - 8 / 4 using standard precedence and integer division. What is the result?
- The exponent operator ^ is right-associative. What is the value of 2 ^ 3 ^ 2?
- How many valid (balanced) bracket sequences of length 8 are there using one type of bracket?
- A grid has the rows ..#.., .##.., ....., #.#.# (where '.' is open and '#' is a wall; all rows have length 5). Starting from the top-left…
- A list holds 1, 1, 2, 2, 2, 3, 1, 1. Consecutive duplicates are merged into one node each. What is the sum of the values left in the list?
- How many distinct strings can be obtained from abc by swapping two adjacent characters at most once (including the original string)?