Data Structures & AlgorithmsModerateGATE CS · DSA
Kadane's algorithm runs on [4, 0, 8, −3, −2, −2, −3, 2, −6]. What is the maximum sum of a non-empty contiguous subarray?
- A. 12
- B. 11
- C. 14
- D. 8
Correct answer: A. 12
Keep cur = max(x, cur + x) and best = max(best, cur). The best is 12; adding all positive numbers (14) ignores that they must be contiguous.
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
- How many characters must be deleted in total from two strings so that they become anagrams of each other? What is printed? python from…
- What does this loop-counting code print? (Python) python n, c = 33, 0 i = 1 while i < n: j = i while j < n: c += 1 j = 2 i += 1 print(c)
- Evaluate an infix expression of single digits with two stacks (operands and operators, usual precedence, left-associative). What is…
- One Lomuto partition (pivot = LAST element; i marks the end of the 'less than pivot' region; when a[j] < pivot, increment i and swap a[i]…
- Insertion sort (ascending; shift larger elements right, one key comparison per inner-loop test, the loop stops at the first element ≤ the…
- One Hoare partition with the FIRST element as pivot (i moves right while a[i] < pivot, j moves left while a[j] > pivot, swap while i < j…