Data Structures & AlgorithmsHardGATE CS · TCS NQT
What is the maximum number of pairwise disjoint non-empty contiguous subarrays of [1, -1, 2, -2, 3, -3, 4] that each have sum zero?
- A. 4
- B. 3
- C. 2
- D. 1
Correct answer: B. 3
Greedy works here: cut as soon as a prefix sum repeats, since ending the current zero-sum block earlier leaves more room. The answer is the number of such cuts.
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 entries of [7, 8, 11, 16, 15, 3] have the largest number of 1-bits in the array?
- Sort all suffixes of "banana" lexicographically. What is the sum of the lengths of the longest common prefixes of each pair of adjacent…
- How many distinct substrings of length 4 does "abcabcabca" contain?
- How many proper borders (non-empty proper prefixes that are also suffixes) does "abaabaaba" have?
- Using z[0] = 0, what is the sum of the Z-function values over all indices of "aabxaayaab"?
- Starting at index 0 of [3, 2, 1, 0, 4], each value a[i] allows a jump of at most a[i] steps forward. Can the last index be reached?