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.
- A. Both A and R are true, and R is the correct explanation of A
- B. Both A and R are true, but R is not the correct explanation of A
- C. A is true, but R is false
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Assertion (A): Binary search on the answer requires that the feasibility predicate is monotone. Reason (R): Only a monotone predicate lets…
- Assertion (A): The greedy 'largest coin first' is optimal for the coin set {1, 3, 4} when the amount is 6. Reason (R): A coin system whose…
- Which of the following statements about the sliding window technique on arrays with NEGATIVE numbers is/are correct? I. A plain…
- The array [7, 2, 5, 10, 8, 3, 6] must be split into exactly 3 contiguous non-empty parts (order kept). What is the smallest possible value…
- An array has 10^5 values and you need the k-th smallest value among all pair sums a[i] + a[j] with i < j. Which approach is the right fit?
- From [4, 9, 2, 7, 5, 1] you may pick any elements (each at most once). What is the minimum number of picks so that the picked sum is at…