Data Structures & AlgorithmsVery hard
What is the maximum number of pairwise DISJOINT contiguous blocks of 1 -1 2 3 -3 4 -4 5 whose sums are all zero? (Blocks must not overlap; uncovered elements are allowed.)
- A. 4
- B. 2
- C. 5
- D. 3
Correct answer: D. 3
Interval DP over prefixes: the best count ending at position t is either the best ending at t−1 or a zero-sum block ending at t plus the best before its start. Counting all zero-sum blocks overlaps them, and a greedy scan can miss a better packing.
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
- How many contiguous subarrays of 2 1 3 2 1 3 3 2 contain EVERY distinct value that appears in the whole array?
- How many contiguous windows of length exactly 4 in 1 3 4 5 2 6 7 1 4 have a sum that is a perfect square?
- How many contiguous subarrays of 1 2 3 4 5 6 7 8 of length at least 2 contain EXACTLY as many odd values as even values?
- What is the length of the shortest contiguous subarray of 1 4 6 3 8 5 2 7 that contains at least 2 odd values AND at least 2 even values?
- How many index pairs i < j in 2 3 1 4 5 2 6 have the closed block A[i..j] strictly increasing (with j > i)?
- How many contiguous windows of length exactly 3 in 4 1 2 3 9 8 6 7 are strictly monotone (increasing or decreasing) AND contain the global…