Data Structures & AlgorithmsModerateGATE CS · TCS NQT

Assertion (A): Kadane's algorithm can be used to find the maximum subarray sum in O(n) time. Reason (R): It keeps the best sum ending at the current index and discards it whenever it becomes negative.

  1. A. Both A and R are true, and R is the correct explanation of A
  2. B. Both A and R are true, but R is not the correct explanation of A
  3. C. A is true, but R is false
  4. D. A is false, but R is true

Correct answer: A. Both A and R are true, and R is the correct explanation of A

Kadane's recurrence cur = max(x, cur + x) is exactly the rule 'restart when the running sum would hurt', and it gives the answer in one pass.

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

Exams that ask this topic