-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestSumContiguousSubarray.java
More file actions
43 lines (39 loc) · 1.04 KB
/
LargestSumContiguousSubarray.java
File metadata and controls
43 lines (39 loc) · 1.04 KB
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
39
40
41
42
43
/**
* Largest Sum of Contiguous Subarray Problem
* @author deenliu
*
* Given a sequence of n real numbers A(1) ... A(n), determine a
* contiguous subsequence A(i) ... A(j) for which the sum of elements in
* the subsequence is maximized.
*
*/
public class LargestSumContiguousSubarray {
public static int largestSumContiguousSubarray(int data[], int size) {
int max = 0;
int currentMax = 0;
int start = 0, end = 0;
for (int i = 0, j = 0; i < size; i++) {
if (currentMax <= 0) {
currentMax = data[i];
j = i;
} else {
currentMax += data[i];
}
if (currentMax > max) {
max = currentMax;
start = j;
end = i;
}
}
System.out.printf("The largest sum of contiguous subarray is %d.\n",
max);
System.out.printf("It is from %d to %d.\n\n", start, end);
return max;
}
public static void testLSCS() {
int[] A = { -2, 11, -4, 13, -5, 2 };
int[] B = { -15, 29, -36, 3, -22, 11, 19, -5 };
largestSumContiguousSubarray(A, A.length);
largestSumContiguousSubarray(B, B.length);
}
}