Answer: ___________
View worked solution
Answer: 29
This is the classic Maximum Subarray Sum problem. We can solve this efficiently in time using Kadane's Algorithm.
Kadane's Algorithm Steps
Let be the given array of size 14:
We maintain two variables:
current_max: The maximum subarray sum ending at the current position.global_max: The overall maximum subarray sum found so far.
We iterate through the array from index to using the recurrence:
| Index () | Element | current_max |
global_max |
|---|---|---|---|
| 0 | -5 | -5 | -5 |
| 1 | -10 | -10 | -5 |
| 2 | 6 | 6 | 6 |
| 3 | 3 | 9 | 9 |
| 4 | -1 | 8 | 9 |
| 5 | -2 | 6 | 9 |
| 6 | 13 | 19 | 19 |
| 7 | 4 | 23 | 23 |
| 8 | -9 | 14 | 23 |
| 9 | -1 | 13 | 23 |
| 10 | 4 | 17 | 23 |
| 11 | 12 | 29 | 29 |
| 12 | -3 | 26 | 29 |
| 13 | 0 | 26 | 29 |
The maximum subarray sum is achieved by the subarray from index to :
Thus, the maximum of is 29.
Free practice · Your answers on this page are not saved.
