Data Structures & AlgorithmsHardGATE CS · TCS NQT

What is the maximum number of pairwise disjoint contiguous subarrays of [2, 3, 1, 4, 2, 3, 5] that each have sum at least 5 (taken from left to right greedily)?

  1. A. 5
  2. B. 4
  3. C. 3
  4. D. 8

Correct answer: B. 4

Close a block as soon as its running sum reaches the threshold; ending a block earlier always leaves at least as much room for the rest. This greedy is optimal for this structure.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic