Answer: ___________
GATE CSE 2019Algorithms › Greedy Method, Dynamic Programming & Algorithm Design TechniquesVerified
GATE CSE 2019 PYQ: Consider a sequence of 14 14 14 elements: A = [ − 5 , − 10 , 6 , 3 , − 1 , − 2 , 13 , 4 , − 9 , − 1 , 4 , 12…
Answer
29
Approach
The quantity S(i, j) asks for the sum of a contiguous block A[i..j], so the task is to find the maximum subarray sum (not the maximum subsequence sum). Since all indices must be contiguous, the classic Kadane dynamic-programming scan computes the optimum in one pass: maintain the best sum ending at the current position and the overall best seen so far.
Applying Kadane's algorithm or direct inspection of contiguous blocks shows the highest attainable sum is achieved by the subarray from index 2 through index 11: 6 + 3 − 1 − 2 + 13 + 4 − 9 − 1 + 4 + 12 = 29. No other contiguous block yields a larger sum, so the answer is 29.
Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.