FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaProgramming/Array/KadaneAlgorithm.java at main · KotlinKing/JavaProgramming · GitHub
KotlinKing
/
JavaProgramming
Public
forked from
karterhhgg/JavaProgramming
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaProgramming
/
Array
/
KadaneAlgorithm.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
29 lines (23 loc) · 1007 Bytes
Breadcrumbs
JavaProgramming
/
Array
/
KadaneAlgorithm.java
Copy path
File metadata and controls
29 lines (23 loc) · 1007 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
public
class
KadaneAlgorithm
{
// Function to find the maximum subarray sum
public
static
int
maxSubArraySum
(
int
[]
arr
) {
int
maxSoFar
=
arr
[
0
];
// Initialize to first element
int
maxEndingHere
=
arr
[
0
];
// Max sum ending at current position
for
(
int
i
=
1
;
i
<
arr
.
length
;
i
++) {
// Either extend current subarray or start new subarray from current element
maxEndingHere
=
Math
.
max
(
arr
[
i
],
maxEndingHere
+
arr
[
i
]);
// Update overall maximum
maxSoFar
=
Math
.
max
(
maxSoFar
,
maxEndingHere
);
}
return
maxSoFar
;
}
// Driver code to test the algorithm
public
static
void
main
(
String
[]
args
) {
int
[]
arr
= {-
2
, -
3
,
4
, -
1
, -
2
,
1
,
5
, -
3
};
System
.
out
.
println
(
"Array elements:"
);
for
(
int
num
:
arr
)
System
.
out
.
print
(
num
+
" "
);
System
.
out
.
println
();
int
maxSum
=
maxSubArraySum
(
arr
);
System
.
out
.
println
(
"Maximum subarray sum is: "
+
maxSum
);
}
}
Back
|
FazBrowse Home
|
New Git URL