diff --git a/Code/C++/kadane_algo.cpp b/Code/C++/kadane_algo.cpp index 0c1f208ce..abc832a68 100644 --- a/Code/C++/kadane_algo.cpp +++ b/Code/C++/kadane_algo.cpp @@ -1,99 +1,90 @@ /* -Kadane's Algorithm is used to solve the maximum subarray sum problem. + Kadane's Algorithm is used to solve the maximum subarray sum problem. -The maximum subarray problem is the task of finding the largest -possible sum of a contiguous subarray. - -We make three cases in this algorithms. + The maximum subarray problem is the task of finding the largest + possible sum of a contiguous subarray. + + We make three cases in this algorithms. -1 - all the array elements are postive - then complete array -sum is the largest subarray sum. + 1 - all the array elements are postive - then complete array + sum is the largest subarray sum. -2 - all the array elemets are negative - then smallest elements -is the largest subarray sum. + 2 - all the array elemets are negative - then smallest elements + is the largest subarray sum. -3 - elements are mix of positive and negative numbers - then we keep adding -elements in the variable until the variable is positive. We keep track of the -variable value after each addition, so that we know the maximum value it rose to, -that maximum value is our answer. + 3 - elements are mix of positive and negative numbers - then we keep adding + elements in the variable until the variable is positive. We keep track of the + variable value after each addition, so that we know the maximum value it rose to, + that maximum value is our answer. */ #include <bits/stdc++.h> #define ll long long using namespace std; + main() { ios_base::sync_with_stdio(false); - cin.tie(NULL); + cin.tie(NULL); ll int n,maxs; // no of elements in the array cin>>n; - - + ll int arr[n],i,sum=0,pos=0,neg=0, maxi = INT_MIN; for(i=0;i<n;i++) { - - // array input + // array input cin>>arr[i]; - // complete sum of the array - sum += arr[i]; + // complete sum of the array + sum += arr[i]; - // no of positive values + // no of positive values if(arr[i] >=0 ) - pos++; + pos++; - // no of negative values + // no of negative values else - neg++; - + neg++; } // if all elements are positive - if(pos == n) - - printf("%lld\n",sum); + if(pos == n) + printf("%lld\n",sum); // if all elements are negative - else if(neg == n) - + else if(neg == n) { - // find the largest element + // find the largest element for(i = 0; i<n; i++) maxi = max(maxi , arr[i]); - // print the largest elements + // print the largest elements printf("%lld\n",maxi); - } + else { ll int left = 0; ll int maxs = 0; for(i=0; i<n; i++) - { - + { // if variable becomes negative, reset left to 0 - if( left + arr[i] <=0 ) - - { + if( left + arr[i] <=0 ) left = 0; - } - + else - // add if the variable will remain positive + // add if the variable will remain positive { left += arr[i]; maxs = max(maxs,left); - // keep track of variable's value to know the maximum - - } + // keep track of variable's value to know the maximum + } } // print the answer