Data Structures & AlgorithmsModerateGATE CS · DSA

Kadane's algorithm runs on [4, 0, 8, −3, −2, −2, −3, 2, −6]. What is the maximum sum of a non-empty contiguous subarray?

  1. A. 12
  2. B. 11
  3. C. 14
  4. D. 8

Correct answer: A. 12

Keep cur = max(x, cur + x) and best = max(best, cur). The best is 12; adding all positive numbers (14) ignores that they must be contiguous.

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

Exams that ask this topic