Data Structures & AlgorithmsHardGATE CS · TCS NQT
What is the maximum sum over all non-empty contiguous subarrays of [3, -8, 4, -1, 2, -6, 5, 1, -7, 4]?
- A. 5
- B. 6
- C. 19
- D. -3
Correct answer: B. 6
Kadane keeps the best sum ending at each index and restarts after a negative run: 4 − 1 + 2 = 5, then after the −6 reset 5 + 1 = 6. The single maximum element 5 is the trap; 19 adds all positives and −3 is the whole-array sum.
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
- For the circular array [5, -3, 4, -6, 2, 3, -1, 4], what is the maximum sum of a non-empty subarray when the array is treated as circular…
- How many index pairs i < j satisfy a[i] + a[j] = 10 for a = [7, 1, 5, 3, 9, 2, 8, 4, 6, 0]?
- What is the length of the longest contiguous subarray of [1, 2, 1, 2, 3, 2, 2, 4, 1, 1, 1] that contains at most 2 distinct values?
- For the positive array [2, 3, 1, 2, 4, 3] and target S = 7, what is the length of the shortest contiguous subarray whose sum is at least 7?
- Five stalls are at positions 1, 2, 4, 8, 9 on a line. Three cows are placed in distinct stalls so that the minimum distance between any…
- A conveyor must ship packages with weights 1, 2, 3, ..., 10 in the given order, in at most 5 days, and loads cannot be reordered. What is…