# Kadane’s Algorithm using Dynamic Programming

»

This is a C++ Program that Solves Kadane’s Algorithm using Dynamic Programming technique.

Problem Description

Maximum Subarray Problem
Given a sequence of n real numbers A(1) … A(n), determine a contiguous subsequence A(i) … A(j) for which the sum of elements in the subsequence is maximized.

Problem Solution

We will proceed in a linear fashion maintaining overall_max and current_max variables. If adding the current element to current_max results in overall maximum value, we will replace the value of overall_max variable. Otherwise, we will proceed furthere.

Expected Input and Output

Case-1:

```
A[]={2,4,6,8}
maximum contiguous subarry= {2, 4, 6, 8}
maximum contiguous subarray sum= 20```

Case-2:

```
A[]={-2, 3, -7, 5, -9}
maximum contiguous subarry= {5}
maximum contiguous subarray sum= 5```

Case-3:

```
A[]={3, -4, 7, -9, 8, 7}
maximum contiguous subarry= {8, 7}
maximum contiguous subarray sum= 15

A[]={3, -4, 9, -8, 8, 7}
maximum contiguous subarry= {9, -8, 8, 7}
maximum contiguous subarray sum= 16```

Case-4:

```
A[]={3, -4, 9, -8, 8, 7}
maximum contiguous subarry= {9, -8, 8, 7}
maximum contiguous subarray sum= 16```
Program/Source Code for Brute force solution

Here is source code of the C++ Program to Solve Kadane’s Algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

1. ` `
2. `#include<iostream>`
3. `using namespace std;`
4. ` `
5. `//This function will check every contiguous subarray and return the sum of maximum subarray`
6. `int maxSubarraySum(int a[], int n)`
7. `{`
8. `    int overall_sum=0;  //overall maximum subarray sum`
9. `    int new_sum;`
10. ` `
11. `    for(int i=0;i<n;i++)`
12. `    {`
13. ` `
14. `        //find the maximum subarray sum of the subarray starting from a[i]`
15. `        new_sum=0;`
16. `        for(int j=i;j<n;j++)`
17. `        {`
18. `            new_sum+=a[j];`
19. ` `
20. `            if(new_sum>overall_sum)`
21. `            {`
22. `                overall_sum=new_sum;`
23. `            }`
24. ` `
25. `        }`
26. `    }`
27. ` `
28. `    return overall_sum;`
29. `}`
30. ` `
31. `int main()`
32. `{`
33. `    int i,n;`
34. ` `
35. `    cout<<"Enter the number of elements in the array  ";`
36. `    cin>>n;`
37. ` `
38. `    int a[n];`
39. ` `
40. `    //read the vector`
41. `    cout<<"enter the elements in the array"<<endl;`
42. `    for(i=0;i<n;i++)`
43. `    {`
44. `        cin>>a[i];`
45. `    }`
46. ` `
47. `    //now,make a call to kadane function to calculate maximum subarray sum`
48. `    cout<<endl<<"The maximum subarray sum for the given array is "<<maxSubarraySum(a,n); `
49. ` `
50. `    return 0;`
51. `}`
Program/Source Code for DP solution

Here is source code of the C++ Program to Solve Kadane’s Algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

1. `#include<iostream>`
2. ` `
3. `using namespace std;`
4. ` `
5. `int kadane(int a[], int n)`
6. `{`
7. `    int overall_sum=0;  //overall maximum subarray sum`
8. `    int new_sum=0;      //sum obtained by including the current element`
9. ` `
10. `    for(int i=0;i<n;i++)`
11. `    {`
12. `        //new_sum is the maximum value out of current element or the sum of current element`
13. `        //and the previous sum`
14. `        new_sum=max(a[i], new_sum+a[i]);`
15. ` `
16. `        //if the calculated value of new_sum is greater than the overall sum,`
17. `        //it replaces the overall sum value`
18. `        overall_sum=max(overall_sum, new_sum);`
19. `    }`
20. ` `
21. `    return overall_sum;`
22. ` `
23. `}`
24. ` `
25. `int main()`
26. `{`
27. `    int i,n;`
28. ` `
29. `    cout<<"Enter the number of elements in the array  ";`
30. `    cin>>n;`
31. ` `
32. `    int a[n];`
33. ` `
34. `    //read the vector`
35. `    cout<<"enter the elements in the array"<<endl;`
36. `    for(i=0;i<n;i++)`
37. `    {`
38. `        cin>>a[i];`
39. `    }`
40. ` `
41. `    //now,make a call to kadane function to calculate maximum subarray sum`
42. `    cout<<endl<<"The maximum subarray sum for the given array is "<<kadane(a,n); `
43. ` `
44. `    return 0;`
45. `}`
Program Explanation

In the main function, we ask the user to input the elements of the array. We pass this array to the function kadane as parameter. This function will calculate the expected result and return it. The returned value will be displayed.

Runtime Test Cases
```
Case-1:
\$ ./a.out
Enter the number of elements in the array 4
enter the elements in the array
2
4
6
8
The maximum subarray sum for the given array is 20```

Sanfoundry Global Education & Learning Series – Dynamic Programming Problems.
To practice all Dynamic Programming Problems, here is complete set of 100+ Problems and Solutions. 