-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKadaneAlgorithm.java
More file actions
32 lines (29 loc) · 956 Bytes
/
KadaneAlgorithm.java
File metadata and controls
32 lines (29 loc) · 956 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
package com.namedAlgos;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class KadaneAlgorithm {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
String[] str = line.trim().split(",");
int[] arr = new int[str.length];
for (int i = 0; i < str.length; i++) {
arr[i] = Integer.parseInt(str[i]);
}
int max = applyKadaneAlgo(arr);
System.out.println(max);
}
private static int applyKadaneAlgo(int arr[]) {
int result = Integer.MIN_VALUE;
int max = 0;
for (int i : arr) {
max = max + i;
if (result < max)
result = max;
if (max < 0)
max = 0;
}
return result;
}
}