Data Structures & AlgorithmsModerateTCS Digital · Infosys SP

Kadane's algorithm runs on [−4, −7, 9, 3, −5, 4, 1, 4, 8]. What is the maximum sum of a non-empty contiguous subarray?

  1. A. 9
  2. B. 24
  3. C. 29
  4. D. 23

Correct answer: B. 24

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

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

Exams that ask this topic