Data Structures & AlgorithmsHardCodeforces Div2 C · TCS NQT

What is the maximum sum of a non-empty contiguous subarray of [4, -6, 3, -1, 7, -2, 5] that contains AT MOST one negative element?

  1. A. 10
  2. B. 7
  3. C. 12
  4. D. 11

Correct answer: A. 10

Let the window hold at most one negative: track the best sums with zero and one negative inside. Allowing none gives a smaller answer, and allowing two gives a larger one.

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

Exams that ask this topic