Data Structures & AlgorithmsVery hardCodeforces Div2 D · GATE CS
Which of the following statements about the sliding window technique on arrays with NEGATIVE numbers is/are correct? I. A plain shrink-from-left window correctly finds the longest subarray with sum at most K. II. A prefix-sum structure (a monotone deque or a sorted map) can find the shortest subarray with sum at least K. III. Counting subarrays with sum exactly K can be done with a prefix-sum frequency map in O(n).
- A. I and II only
- B. I only
- C. I, II and III
- D. II and III only
Correct answer: D. II and III only
I is false: adding a negative element can reduce the sum, so shrinking from the left does not keep the window monotone. II and III are standard prefix-sum techniques that handle negative 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
- The array [7, 2, 5, 10, 8, 3, 6] must be split into exactly 3 contiguous non-empty parts (order kept). What is the smallest possible value…
- An array has 10^5 values and you need the k-th smallest value among all pair sums a[i] + a[j] with i < j. Which approach is the right fit?
- From [4, 9, 2, 7, 5, 1] you may pick any elements (each at most once). What is the minimum number of picks so that the picked sum is at…
- How many index triples i < j < k in [-2, -1, 0, 1, 2, 3] have a sum of exactly 0?
- How many contiguous subarrays of [1, 2, 3, 4] have a sum that is a perfect square?
- A string "cdeab" is rotated left by some number of steps k (0 <= k < 5) to become "abcde". What is the smallest such k?