forked from ghostmkg/programming-language
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKaden_Algo.java
More file actions
Latest commit
21 lines (18 loc) · 515 Bytes
/
Copy pathKaden_Algo.java
File metadata and controls
21 lines (18 loc) · 515 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
publicclassKaden_Algo {
publicstaticvoidmain(String[] args) {
int[] arr= { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
System.out.println(Kadane(arr));
}
publicstaticintKadane(int[] arr) {
intans = Integer.MIN_VALUE;
intprevsum = 0;
for (inti = 0; i < arr.length; i++) {
prevsum += arr[i];
ans = Math.max(ans, prevsum);
if (prevsum < 0) {
prevsum = 0;
}
}
returnans;
}
}