MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / main

Function main

CSES_Problems/Maximum_Subarray_Sum/maxsubsum.cpp:11–29  ·  view source on GitHub ↗

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 */

Source from the content-addressed store, hash-verified

9Each time we get a positive sum compare it with maxVal and update max_so_far if it is greater than maxVal
10*/
11int 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}

Callers

nothing calls this directly

Calls 1

maxFunction · 0.85

Tested by

no test coverage detected