Data Structures & AlgorithmsVery hard
How many DISTINCT values appear among the sums of all non-empty contiguous subarrays of [1, 2, 3, 1, 2, 3]?
- A. 21
- B. 7
- C. 11
- D. 3
Correct answer: C. 11
Count the set of sums, not the number of subarrays: many different subarrays share a sum here. Total subarrays counts with multiplicity, and the set of prefix sums (including 0) is a different collection from the set of subarray sums.
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 [6, 10, 15, 4, 9] have gcd equal to 1?
- How many contiguous subarrays of [1, 3, 5, 7, 2, 4] have a sum that is a perfect square (0 counts as a square)?
- How many index pairs i < j in [5, 1, 9, 4, 8, 3, 7] satisfy T[i] − T[j] = 4?
- How many contiguous subarrays of [1, 2, 3, 1, 2, 4, 5, 0, 6] are strictly increasing and have length at least 2?
- How many contiguous subarrays of [2, 5, 8, 3, 4, 7, 6, 1] of length at least 2 have consecutive elements of alternating parity (odd and…
- How many contiguous subarrays of positive integers [2, 1, 4, 3, 1, 2] have a sum at most 6?