forked from iamAnki/Codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKadanesAlgorithm.java
More file actions
29 lines (27 loc) · 805 Bytes
/
KadanesAlgorithm.java
File metadata and controls
29 lines (27 loc) · 805 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
import java.util.*;
public class KadanesAlgorithm
{
private static int kadaneAlgorithmSolution(ArrayList<Integer> list, int n)
{
int maxSum = 0;
int currentSum = 0;
for (int i = 0; i < n; i++) {
currentSum = currentSum + list.get(i);
if (currentSum > maxSum)
maxSum = currentSum;
if (currentSum < 0)
currentSum = list.get(i);
}
return maxSum;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
list.add(sc.nextInt());
}
System.out.println(kadaneAlgorithmSolution(list, n));
}
}