Data Structures & AlgorithmsModerateGATE CS · DSA

Kadane's algorithm is applied to an array whose elements are ALL negative, using the version cur = max(x, cur + x), best = max(best, cur), with best initialised to the first element. What does it return?

  1. A. 0
  2. B. The smallest element
  3. C. The sum of all elements
  4. D. The largest (least negative) element

Correct answer: D. The largest (least negative) element

With every element negative, extending a subarray only lowers the sum, so the best non-empty subarray is the single largest element. The version that resets cur to 0 and starts best at 0 would wrongly return 0.

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

Exams that ask this topic