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?
- A. 0
- B. The smallest element
- C. The sum of all elements
- 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.
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
- Remove the n-th node from the END of a linked list in one pass, using a dummy node and a gap of n between two pointers. What is printed?…
- Z-array: z[i] is the length of the longest common prefix of s and s[i:], with z[0] defined as 0. What is printed? python s = "bbaaaabab" z…
- Asteroids move along a line: positive values move right, negative move left; on collision the smaller (by absolute value) explodes, equal…
- A difference array is used for q range-increment updates followed by one final read of all n values. What is the total time?
- Top-down merge sort (split at mid = n // 2, recurse, then merge, stopping comparisons when one side runs out) sorts [10, 29, 19, 28, 32…
- The Boyer–Moore majority vote (candidate/counter, no verification pass) runs on [3, 3, 3, 3, 7, 3, 7, 5, 5]. What candidate and final…