forked from Algo-Phantoms/Algo-Tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kadane_algo.cpp
125 lines (86 loc) · 2.17 KB
/
kadane_algo.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
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.
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.
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);
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
cin>>arr[i];
// complete sum of the array
sum += arr[i];
// no of positive values
if(arr[i] >=0 )
pos++;
// no of negative values
else
neg++;
}
// if all elements are positive
if(pos == n)
printf("%lld\n",sum);
// if all elements are negative
else if(neg == n)
{
// find the largest element
for(i = 0; i<n; i++)
maxi = max(maxi , arr[i]);
// 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 )
{
left = 0;
}
else
// add if the variable will remain positive
{
left += arr[i];
maxs = max(maxs,left);
// keep track of variable's value to know the maximum
}
}
// print the answer
printf("%lld\n",maxs);
}
}
/*
Testcase
Input :
5
4 1 -3 7 12
Output : 21
Input :
6
-1 -4 -5 8 7 9
Output : 24
Time complexity : O(n) in average case
Space complexity : O(1)
*/