diff --git a/solutions/java/LargestContinuousSum.java b/solutions/java/LargestContinuousSum.java new file mode 100644 index 0000000..daa9964 --- /dev/null +++ b/solutions/java/LargestContinuousSum.java @@ -0,0 +1,23 @@ +public class LargestContinuousSum { + public static void main(String[] args) { + LargestContinuousSum lcd = new LargestContinuousSum(); + int[] input = { 1, -1, 2, -5, 10, 15, -10, 5 }; + System.out.print(lcd.getSum(input)); + } + + public int getSum(int[] input) { + int currentMax = input[0]; + int finalMax = input[0]; + for (int i = 1; i < input.length; i++) { + if (currentMax + input[i] > input[i]) + currentMax = currentMax + input[i]; + else + currentMax = input[i]; + + if (currentMax > finalMax) + finalMax = currentMax; + + } + return finalMax; + } +} \ No newline at end of file