Data Structures & AlgorithmsVery hard
When covering all distinct values of an array with a subarray, why does a sliding window track counts rather than a single flag?
- A. Counts are needed only to sort the values.
- B. A flag is always enough because values never repeat.
- C. A flag cannot tell when a value leaves the window, so counts are needed to know whether the window still contains every value.
- D. Counts are required because windows can only have length one.
Correct answer: C. A flag cannot tell when a value leaves the window, so counts are needed to know whether the window still contains every value.
When a value leaves the window its count drops, and the window stops covering the set only when that count hits zero. A boolean flag loses this information.
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
- Why does the sum-of-suffix-maxima total equal a simple right-to-left scan?
- When counting windows whose sum is a perfect square, why is the square test exact only with integer square roots?
- In the 'longest subarray with a single removal' interview problem, why is subtracting the minimum from the total a common mistake?
- Which property lets the 'longest subarray where every adjacent pair differs by exactly 1' be found in one pass?
- When computing the minimum adjacent swaps by breadth-first search over arrangements, why does the first time a target is reached give the…
- Intervals [(1, 3), (2, 5), (4, 6), (3, 4), (6, 8), (5, 7)] are closed integer ranges. Choose as many as possible so that every integer…