-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestSum.java
More file actions
59 lines (46 loc) · 1.15 KB
/
LargestSum.java
File metadata and controls
59 lines (46 loc) · 1.15 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class LargestSum {
int[] array;
int numElements;
private int maxSubsequenceSum() {
int i;
int maxSum;
int tmpSum;
maxSum = array[0];
tmpSum = array[0];
for (i = 1; i < numElements; i++) {
if (tmpSum + array[i] > array[i]) {
tmpSum+=array[i];
} else {
tmpSum = array[i];
}
if (tmpSum > maxSum) {
maxSum = tmpSum;
}
}
return maxSum;
}
public LargestSum(String filename) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(filename));
String line;
int i;
while((line = br.readLine()) != null) {
String[] numbers = line.split(",");
numElements = numbers.length;
array = new int[numElements];
for (i = 0; i < numElements; i++) {
array[i] = Integer.valueOf(numbers[i]).intValue();
}
System.out.println(maxSubsequenceSum());
}
}
public static void main (String[] args) throws IOException {
if (args.length != 1) {
System.out.println("Unsupported number of parameters passed. Exiting.");
System.exit(1);
}
LargestSum ls = new LargestSum(args[0]);
}
}