Data Structures & AlgorithmsModerateProduct-company MCQ pattern

Kadane's algorithm is run on [3, −4, 5, −1, 2, −6, 4]. What maximum contiguous subarray sum does it return?

  1. A. 14
  2. B. 5
  3. C. 6
  4. D. 3

Correct answer: C. 6

The best subarray is [5, −1, 2] with sum 6. Running sums restart after 3 − 4 = −1 < 0; then 5, 4, 6 (best), 0, 4. Adding all the positives (14) ignores contiguity, 5 is just the largest element and 3 is the whole-array sum.

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

Exams that ask this topic