Data Structures & AlgorithmsModerateCodeforces Div2 B-D · ICPC regional
How many non-empty contiguous subarrays of [2, -1, -1, 3, -2, 1, 1, -2, 0] have a sum of exactly 0?
- A. 9
- B. 8
- C. 4
- D. 1
Correct answer: A. 9
Prefix sums 0, 2, 1, 0, 3, 1, 2, 3, 1, 1: value 1 occurs 4 times (6 pairs), and 0, 2, 3 twice each, so 6 + 1 + 1 + 1 = 9. Forgetting the initial prefix 0 gives 8; 4 counts the repeated prefix values, not pairs, and 1 counts only the single 0 element.
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
- What is the maximum sum over all non-empty contiguous subarrays of [3, -8, 4, -1, 2, -6, 5, 1, -7, 4]?
- For the circular array [5, -3, 4, -6, 2, 3, -1, 4], what is the maximum sum of a non-empty subarray when the array is treated as circular…
- How many index pairs i < j satisfy a[i] + a[j] = 10 for a = [7, 1, 5, 3, 9, 2, 8, 4, 6, 0]?
- What is the length of the longest contiguous subarray of [1, 2, 1, 2, 3, 2, 2, 4, 1, 1, 1] that contains at most 2 distinct values?
- For the positive array [2, 3, 1, 2, 4, 3] and target S = 7, what is the length of the shortest contiguous subarray whose sum is at least 7?
- Five stalls are at positions 1, 2, 4, 8, 9 on a line. Three cows are placed in distinct stalls so that the minimum distance between any…