Question
Consider a sequence of 14 elements: .
The subsequence sum . Determine the maximum of , where . (Divide and conquer approach may be used.)
Answer: ________.
Official answer and solution
- Official answer
- 29
- From
- Official GATE 2019 answer key
Solution
Use Kadane's algorithm to compute the maximum contiguous subarray sum in linear time.
The given array of 14 elements is:
We compute the maximum subarray sum using Kadane's algorithm, maintaining current_sum and max_so_far:
- :
- :
- :
- :
- :
- :
- :
- :
- :
- :
- :
- :
- :
- :
The maximum contiguous subsequence sum is , achieved by subarray from index 2 to index 11:
29
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2019 Computer Science and Information Technology (CS)
- Question
- 35 of 65, printed as CS Q.25
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.