-
Notifications
You must be signed in to change notification settings - Fork 91
/
KadanesAlgo.java
38 lines (31 loc) · 1.21 KB
/
KadanesAlgo.java
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
33
34
35
36
37
38
class KadanesAlgo
{
// Function to find the maximum sum of a contiguous subarray
// in a given integer array
public static int kadane(int[] A)
{
// stores the maximum sum subarray found so far
int maxSoFar = 0;
// stores the maximum sum of subarray ending at the current position
int maxEndingHere = 0;
// traverse the given array
for (int i: A)
{
// update the maximum sum of subarray "ending" at index `i` (by adding the
// current element to maximum sum ending at previous index `i-1`)
maxEndingHere = maxEndingHere + i;
// if the maximum sum is negative, set it to 0 (which represents
// an empty subarray)
maxEndingHere = Integer.max(maxEndingHere, 0);
// update the result if the current subarray sum is found to be greater
maxSoFar = Integer.max(maxSoFar, maxEndingHere);
}
return maxSoFar;
}
public static void main(String[] args)
{
int[] A = { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
System.out.println("The sum of contiguous subarray with the " +
"largest sum is " + kadane(A));
}
}