FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Dynamic-Programming/maxSubArrSum.cpp at master · shiningflash/Dynamic-Programming · GitHub
shiningflash
/
Dynamic-Programming
Public
Notifications
You must be signed in to change notification settings
Fork
2
Star
2
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Dynamic-Programming
/
maxSubArrSum.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
32 lines (27 loc) · 979 Bytes
Breadcrumbs
Dynamic-Programming
/
maxSubArrSum.cpp
Copy path
File metadata and controls
32 lines (27 loc) · 979 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//
kadane algorithm :: TC - O(N)
//
@author :: Amirul Islam
#
include
<
bits/stdc++.h
>
using
namespace
std
;
int
maxSubArrSum
(
int
a[],
int
sz) {
int
cur_max = a[
0
], global_max = a[
0
];
for
(
int
i =
1
; i < sz; i++) {
cur_max =
max
(a[i], cur_max + a[i]);
global_max =
max
(global_max, cur_max);
}
return
global_max;
}
int
maxSubArrSum_withStartEnd
(
int
a[],
int
sz) {
int
cur_max =
0
, global_max =
INT_MIN
,
start
(
0
),
end
(
0
),
s
(
0
);
for
(
int
i =
0
; i < sz; i++) {
cur_max = cur_max + a[i];
if
(global_max < cur_max) global_max = cur_max, start = s, end = i;
if
(cur_max <
0
) cur_max =
0
, s = i +
1
;
}
printf
(
"
\n
Max Sub Array Sum = %d
\n
Start = %d
\t
End = %d
\n
"
, global_max, start, end);
}
int
main
() {
int
a[] = {-
2
, -
3
,
4
, -
1
, -
2
,
1
,
5
, -
3
};
int
max_sum =
maxSubArrSum
(a,
sizeof
(a)/
sizeof
(a[
0
]));
printf
(
"
Max Sub Array Sum = %d
"
, max_sum);
maxSubArrSum_withStartEnd
(a,
sizeof
(a)/
sizeof
(a[
0
]));
}
Back
|
FazBrowse Home
|
New Git URL