Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit cf1df8e

Browse files
committed
Largest continuous sum Java.
1 parent f3bf77c commit cf1df8e

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class LargestContinuousSum {
2+
public static void main(String[] args) {
3+
LargestContinuousSum lcd = new LargestContinuousSum();
4+
int[] input = { 1, -1, 2, -5, 10, 15, -10, 5 };
5+
System.out.print(lcd.getSum(input));
6+
}
7+
8+
public int getSum(int[] input) {
9+
int currentMax = input[0];
10+
int finalMax = input[0];
11+
for (int i = 1; i < input.length; i++) {
12+
if (currentMax + input[i] > input[i])
13+
currentMax = currentMax + input[i];
14+
else
15+
currentMax = input[i];
16+
17+
if (currentMax > finalMax)
18+
finalMax = currentMax;
19+
20+
}
21+
return finalMax;
22+
}
23+
}

0 commit comments

Comments
 (0)