-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKadanealgo.cpp
More file actions
34 lines (34 loc) · 954 Bytes
/
Copy pathKadanealgo.cpp
File metadata and controls
34 lines (34 loc) · 954 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Kadane algo helps us to solve questions like maximum/minimum subarraysum (subarray will be in Contiguous) in an array
#include<bits/stdc++.h>
using namespace std;
// defining a function to return maximum subarray sum
int maxsubarraysum(int arr[],int n)
{
// initialize two variables as follows
int maxsum=INT_MIN;
int currsum=0;
for(int i=0;i<n;i++)
{
// we will keep adding element to 'currsum' until it becomes negative
currsum+=arr[i];
// asinging the value of currsum to maxsum if it's greater
if(currsum>maxsum)
maxsum=currsum;
// as soon as cursum's value was found negative,make its value zero
if(currsum<0)
currsum=0;
}
return maxsum;
}
int main()
{
// taking input of size of array and its elements
int n;cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<maxsubarraysum(arr,n)<<endl;
return 0;
}