From f62d66a779594249e92c50a474f765c787210c12 Mon Sep 17 00:00:00 2001 From: Ayushi Bisht <94152085+AyuBisht@users.noreply.github.com> Date: Wed, 12 Jan 2022 12:22:41 +0530 Subject: [PATCH] fixed indentation and blank lines --- Code/C++/kadane_algo.cpp | 79 ++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 44 deletions(-) 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 #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>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