Data Structures & AlgorithmsModerateTCS NQT · GATE CS
How many DISTINCT values appear among the sums of all non-empty contiguous subarrays of [2, -1, 3, 0, -2, 4]?
- A. 21
- B. 10
- C. 7
- D. 9
Correct answer: D. 9
Count the set of subarray sums rather than subarrays: several subarrays can have the same sum (for example the zero-sum ones), so the distinct count is smaller than the number of subarrays.
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 unordered pairs of positions (i < j) in "abababcabab" have the same 2-letter substring starting there (s[i:i+2] == s[j:j+2])?
- People stand in a row with heights [3, 1, 4, 2, 5, 2]. Two people can see each other if every person strictly between them is shorter than…
- How many contiguous subarrays of [3, 1, 4, 1, 5, 9, 2] have their FIRST element equal to their minimum (ties allowed)?
- A linked list holds the values [1, 2, 3, 4, 5, 6, 7, 8]. Reverse every complete group of 3 consecutive nodes; a final group with fewer…
- You may delete one contiguous block from [4, -9, 3, 5, -2, 7, -8, 6] (possibly empty block) and keep the prefix before it and the suffix…
- What is the length of the longest common prefix of the two strings obtained by taking all cyclic rotations of "banana" and keeping only…