Data Structures & AlgorithmsVery hardCodeforces Div2 D · LeetCode-style
What is the length of the shortest contiguous subarray of [2, -1, 2, 3, -4, 5, 1] whose sum is at least 6? Negative numbers are present, and if no subarray works, the answer is −1.
- A. 4
- B. 2
- C. 3
- D. 1
Correct answer: B. 2
With negative numbers a shrinking window can discard a valid start. Use prefix sums with a monotone deque: for each end, pop starts whose prefix is no larger than the current prefix, and check the smallest valid start greedily.
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
- A sorted array of distinct values was rotated some number of times. Which technique finds the minimum element in O(log n) time, and why…
- In one operation you may increase any single element by 1. What is the minimum total number of operations needed to make the array [1, 1…
- Choose two NON-OVERLAPPING non-empty contiguous subarrays of [4, -1, 2, 6, -3, 5, -2] (the first must end before the second starts) and…
- In how many ways can a 2 × 9 board be tiled exactly with 2 × 1 dominoes (placed vertically) and 2 × 2 squares, with no overlaps and no…
- How many integers x with 1 ≤ x ≤ 1000 are perfect squares or perfect cubes (or both)?
- Intervals (inclusive integer ranges) are [(1, 3), (2, 5), (4, 9), (6, 7), (8, 12), (9, 10)]. What is the minimum number of intervals whose…