Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed indentation and blank lines #2103

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 35 additions & 44 deletions Code/C++/kadane_algo.cpp
Original file line number Diff line number Diff line change
@@ -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