Data Structures & AlgorithmsHard
How many contiguous subarrays of [2, 1, 3, 1, 1, 2, 4, 1] have each element sum equal to its length (sum of (a[i] − 1) over the subarray is 0)?
- A. 36
- B. 6
- C. 4
- D. 5
Correct answer: D. 5
Subtracting 1 from every element turns the condition into a zero-sum subarray of the new array, which is then a prefix-sum counting problem. Forgetting the initial prefix 0 undercounts, and the other options test a different equation.
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, 2, 3, 3, 4] contain exactly 2 distinct values?
- In the array [2, 7, 3, 7, 1, 4], how many contiguous subarrays have their LEFT-most element equal to the maximum of the subarray (ties…
- How many contiguous subarrays of [1, 2, 1, 3, 2, 2, 1] have equal first and last elements and length at least 2?
- Counting subarrays whose sum is divisible by k with prefix remainders, which statement is TRUE?
- Two pointers count subarrays of POSITIVE integers with sum at most K. Why can the left pointer only move forward as the right pointer moves?
- When summing the MAXIMUM over all subarrays with repeated values, the standard contribution method uses one strict and one non-strict…