Data Structures & AlgorithmsVery hard
Which statement about counting subarrays with EXACTLY k distinct values is correct?
- A. exactly(k) = atMost(k) − atMost(k+2)
- B. exactly(k) = atMost(k) − atMost(k−1)
- C. exactly(k) = atMost(k)
- D. exactly(k) = atMost(k−1) + atMost(k)
Correct answer: B. exactly(k) = atMost(k) − atMost(k−1)
Every subarray with at most k distinct values either has exactly k or at most k−1. Subtracting the at-most-(k−1) count from the at-most-k count leaves exactly the subarrays with k distinct values.
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
- Positive integers, two pointers count subarrays with product less than K. Why is right − left + 1 added for each right end?
- Which statement about inversion counting with repeated values is correct?
- Houses stand in a circle with values [5, 1, 9, 8, 4, 6, 2, 7]. Adjacent houses cannot both be robbed, and the first and last houses are…
- A staircase has 8 steps with costs [25, 14, 7, 32, 2, 25, 28, 39]. You may start on step 0 or step 1, and each move climbs 1 or 2 steps…
- In how many different ORDERED ways can 10 be written as a sum of parts taken from {1, 3, 4}? Sums that differ only in the order of parts…
- Each digit string maps to letters with 1 to A ... 26 to Z. The string "2262012" is split into groups of one or two digits, and each group…