forked from amantyagi22/dynamic-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxSumSubarray.cpp
More file actions
65 lines (61 loc) · 1.46 KB
/
maxSumSubarray.cpp
File metadata and controls
65 lines (61 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<iostream>
using namespace std;
/*
Space optimized solution
|| Kadane's algorithm ||
This will handle all special cases
including all negative elements
*/
int kadane(int* arr,int n){
int current_sum, max_sum;
current_sum = max_sum = arr[0];
for(int i=1;i<n;i++){
if(current_sum >= 0){
current_sum+=arr[i];
}else{
current_sum = arr[i];
}
max_sum = max(max_sum,current_sum);
}
return max_sum;
}
//Top down approach
//Kadane in recursion+memoization
int maxSum(int* arr,int n,int s,int* dp){
if(s==0){
return dp[s] = arr[s];
}
if(dp[s]!=0){
return dp[s];
}
int smallOutput = maxSum(arr,n,s-1,dp);
int cntu = smallOutput + arr[s];
//Check for previous sum to know
//Wheter to continue or start a new one
int ans = (smallOutput>=0?cntu:arr[s]);
return dp[s] = ans;
}
//Bottom Up approach
//Kadane in dp
int maxSumdp(int* arr,int n){
int max_so_far, dp[1000] = {0};
max_so_far = dp[0] = arr[0];
for(int i=1;i<n;i++){
if(dp[i-1] >= 0){
dp[i]=dp[i-1]+arr[i];
}else{
dp[i] = arr[i];
}
max_so_far = max(max_so_far,dp[i]);
}
return max_so_far;
}
int main(){
int n; cin >> n;
int arr[n];
for(int i=0;i<n;i++) cin >> arr[i];
int dp[100] = {0};
cout << maxSum(arr,n,n-1,dp) << endl;
cout << maxSumdp(arr,n) << endl;
cout << kadane(arr,n) << endl;
}