Data Structures & AlgorithmsModerate
How many contiguous subarrays of [2, 4, 1, 3, 5, 2] have a sum in the closed range [5, 9]?
- A. 6
- B. 8
- C. 7
- D. 14
Correct answer: B. 8
Closed range count equals at-most(R) minus at-most(L − 1), which is the same as at-most(R) minus strictly-less-than(L). Subtracting at-most(L) removes the subarrays whose sum equals L, which must stay.
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
- In the array [2, 3, 1, 2, 4, 3] (all positive), what is the length of the shortest contiguous subarray whose sum is at least 7?
- How many contiguous subarrays of [1, 5, 2, 8, 3, 7] have a sum divisible by their own length?
- For [2, 3, 4, 6, 9, 5, 12], how many index pairs i < j have the product BB[i]·BB[j] divisible by 6?
- How many contiguous subarrays of [5, 5, 3, 3, 6, 6, 6] have XOR equal to 0?
- How many index pairs i < j in [1, 5, 3, 3, 3, 7, 2, 4, 6] have DD[i] + DD[j] = 6? Equal values at different indices are different pairs.
- 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…