Kaden's Algorithm's Simple idea of the Kadane’s algorithm is to look for all positive contiguous segments of the array (curr_max is used for this). And keep track of maximum sum contiguous segment among all positive segments (maxVal is used for this). Each time we get a positive sum compare it with maxVal and update max_so_far if it is greater than maxVal */
| 9 | Each time we get a positive sum compare it with maxVal and update max_so_far if it is greater than maxVal |
| 10 | */ |
| 11 | int main(){ |
| 12 | int n; |
| 13 | cin>>n; |
| 14 | long long arr[n]; |
| 15 | for(int i = 0 ;i<n ;i++) |
| 16 | cin>>arr[i]; |
| 17 | |
| 18 | long long maxVal = arr[0]; |
| 19 | long long curr_max = arr[0]; |
| 20 | |
| 21 | for(int i = 1 ; i<n ;i++){ |
| 22 | curr_max = max(curr_max+arr[i], arr[i]); |
| 23 | maxVal = max(maxVal, curr_max); |
| 24 | } |
| 25 | cout<<maxVal; |
| 26 | cout<<"\n"; |
| 27 | |
| 28 | |
| 29 | } |