forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLargestSumContiguousSubArray.java
60 lines (52 loc) · 2.06 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
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
60
package com.rampatra.arrays;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 5/28/15
* @time: 12:44 PM
*/
public class LargestSumContiguousSubArray {
/**
* Based on Kadane's Algorithm. Doesn't work when all
* elements in array {@param a} are negative.
*
* @param a
* @return
*/
public static int getLargestSumOfContiguousSubArray(int[] a) {
int maxSum = 0, maxSumTillIndex = 0;
for (int i = 0; i < a.length; i++) {
maxSumTillIndex += a[i]; // keep on adding elements
if (maxSumTillIndex < 0) {
maxSumTillIndex = 0; // once the sum is less than 0 restart adding elements from next index
} else if (maxSumTillIndex > maxSum) {
maxSum = maxSumTillIndex;
}
}
return maxSum;
}
/**
* Below algorithm works even when all elements in array {@param a} are negative.
*
* @param a
* @return
*/
public static int getLargestSumOfContiguousSubArrayWhenAllNosNegative(int[] a) {
int maxSum = a[0], maxSumTillIndex = a[0];
for (int i = 1; i < a.length; i++) {
maxSumTillIndex = Math.max(a[i], maxSumTillIndex + a[i]);
maxSum = Math.max(maxSum, maxSumTillIndex);
}
return maxSum;
}
public static void main(String[] args) {
System.out.println(getLargestSumOfContiguousSubArray(new int[]{-2, 1, -3, 4, 5, -1, 4}));
System.out.println(getLargestSumOfContiguousSubArray(new int[]{2, -1, -3, 4, -5, 1, 4}));
// kadane's algorithm doesn't work if all no.s are -ve
System.out.println(getLargestSumOfContiguousSubArray(new int[]{-2, -1, -3, -4, -5, -1, -4}));
System.out.println(getLargestSumOfContiguousSubArrayWhenAllNosNegative(new int[]{-2, 1, -3, 4, 5, -1, 4}));
System.out.println(getLargestSumOfContiguousSubArrayWhenAllNosNegative(new int[]{2, -1, -3, 4, -5, 1, 4}));
System.out.println(getLargestSumOfContiguousSubArrayWhenAllNosNegative(new int[]{-2, -1, -3, -4, -5, -1, -4}));
}
}