forked from iiitv/algos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LargestSumContiguousSubarray.java
32 lines (28 loc) · 1.12 KB
/
LargestSumContiguousSubarray.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
/*
* Implementation of famous dynamic programming problem
* Largest Sum Contiguous Subarray
* Kadane Algorithm
* Time complexity O(n)
*/
public class LargestSumContiguousSubarray {
public static int largestSumContiguousSubarray(int[] array) { // maximum sum method implemention
int prevSum;
int currentSum;
int i;
prevSum = array[0]; // initialize current sum amd previous sum
currentSum = array[0];
for (i = 1; i < array.length; i++) {
currentSum += array[i]; // add values in current sum
if (currentSum < 0) { // if current sum is negative , make it zero
currentSum = 0;
} else if (currentSum > prevSum) { // if current sum is greate than previous sum
prevSum = currentSum; // update previous sum
}
}
return prevSum;
}
public static void main(String[] args) {
int[] array = new int[] {-2, 1, -3, 4, -1, 2, 1, -5, 4};
System.out.println("Largest Sum of Contiguous Subarray:" + "\t" + largestSumContiguousSubarray(array));
}
}