Data Structures & AlgorithmsVery hardCodeforces Div2 B-D · ICPC regional
For the positive array [2, 1, 3, 4, 1], how many contiguous subarrays have a sum at most 5?
- A. 8
- B. 7
- C. 15
- D. 5
Correct answer: A. 8
With positive elements the valid left ends for each right end form a range, so adding window lengths gives 1 + 2 + 2 + 1 + 2 = 8. Using < 5 instead of ≤ 5 gives 7, 15 counts every subarray and 5 only the single elements.
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 [1, 2, 1, 3, 4, 2, 3] contain exactly 3 distinct values?
- How many contiguous subarrays of [2, 8, 3, 2, 6, 1] have a product that is a perfect square?
- What is the minimum number of characters that must be deleted from "abcbdab" to make it a palindrome?
- How many pairs of non-overlapping equal substrings of length 2 exist in "abababab" (positions i < j with j >= i + 2)?
- In the text "aaabaaaabaaa", count the occurrences of "aaa" whose starting index is a prime number (0-indexed).
- What is the sum over all adjacent pairs of [4, 2, 5, 1, 3] of the smaller of the two values?