Data Structures & AlgorithmsHard
How many contiguous subarrays of [1, 2, 1, 2, 3, 3, 4] contain exactly 2 distinct values?
- A. 10
- B. 18
- C. 7
- D. 17
Correct answer: A. 10
Exactly k distinct values equals at-most-k minus at-most-(k−1). The mix of 2 and 3 distinct values in the last option is a classic off-by-one error with the difference trick.
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
- In the array [2, 7, 3, 7, 1, 4], how many contiguous subarrays have their LEFT-most element equal to the maximum of the subarray (ties…
- How many contiguous subarrays of [1, 2, 1, 3, 2, 2, 1] have equal first and last elements and length at least 2?
- Counting subarrays whose sum is divisible by k with prefix remainders, which statement is TRUE?
- Two pointers count subarrays of POSITIVE integers with sum at most K. Why can the left pointer only move forward as the right pointer moves?
- When summing the MAXIMUM over all subarrays with repeated values, the standard contribution method uses one strict and one non-strict…
- Which statement about subarrays with XOR equal to 0 is CORRECT?