Data Structures & AlgorithmsHardGATE CS · TCS NQT

What is the maximum sum over all non-empty contiguous subarrays of [3, -8, 4, -1, 2, -6, 5, 1, -7, 4]?

  1. A. 5
  2. B. 6
  3. C. 19
  4. D. -3

Correct answer: B. 6

Kadane keeps the best sum ending at each index and restarts after a negative run: 4 − 1 + 2 = 5, then after the −6 reset 5 + 1 = 6. The single maximum element 5 is the trap; 19 adds all positives and −3 is the whole-array sum.

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

Exams that ask this topic