|
| 1 | +// Java program for Kadane's Algorithms |
| 2 | + |
| 3 | +//### Description :- |
| 4 | + |
| 5 | +//The idea behind the implementation of Kadane’s algorithm is to peek for all positive contiguous segments of the array (max_ending_here is used for this). |
| 6 | +//Keep track of the maximum sum contiguous segment among all positive segments (max_so_far is used for this). |
| 7 | +//Each time we get a positive-sum compare it with max_so_far and update max_so_far if it is greater than max_so_far. |
| 8 | + |
| 9 | + |
| 10 | +class algo{ |
| 11 | + |
| 12 | + static void Kadanes(int a[], int size) |
| 13 | + { |
| 14 | + int max_so_far = Integer.MIN_VALUE, |
| 15 | + max_ending_here = 0,start = 0, |
| 16 | + end = 0, s = 0; |
| 17 | + |
| 18 | + for (int i = 0; i < size; i++) |
| 19 | + { |
| 20 | + // loop to iterate and find the max_ending_here. |
| 21 | + max_ending_here += a[i]; |
| 22 | + |
| 23 | + if (max_so_far < max_ending_here) |
| 24 | + //condition to find the start and end indexes of the sub-array |
| 25 | + { |
| 26 | + max_so_far = max_ending_here; |
| 27 | + start = s; |
| 28 | + end = i; |
| 29 | + } |
| 30 | + |
| 31 | + if (max_ending_here < 0) |
| 32 | + { |
| 33 | + max_ending_here = 0; |
| 34 | + s = i + 1; |
| 35 | + } |
| 36 | + } |
| 37 | + // PRINT THE MAXIMUM CONTIGUOUS SUM WITH THE STARTING AND ENDING INDEXES. |
| 38 | + System.out.println("Maximum contiguous sum is " |
| 39 | + + max_so_far); |
| 40 | + System.out.println("Starting index " + start); |
| 41 | + System.out.println("Ending index " + end); |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + public static void main(String[] args) |
| 46 | + { |
| 47 | + int a[] = { 10, 16, -2, 8, 5, 9, -7, 3 }; |
| 48 | + int n = a.length; |
| 49 | + // CALLING OUR FUNCTION TO IMPLEMENT THE ALGORITH ON THE ABOVE ARRAY a[] |
| 50 | + Kadanes(a, n); |
| 51 | + } |
| 52 | +} |
| 53 | +// A SAMPLE TEST CASE :- |
| 54 | + |
| 55 | +// Here is a test cases to show the implementation of the above stated Algorithm:- |
| 56 | + |
| 57 | +// Lets take the below set of elements in an array as example: |
| 58 | +// {-2, -3, 4, -1, -2, 1, 5, -3} |
| 59 | + |
| 60 | +// max_so_far = max_ending_here = 0 |
| 61 | + |
| 62 | +// for i=0, a[0] = -2 |
| 63 | +// max_ending_here = max_ending_here + (-2) |
| 64 | +// Set max_ending_here = 0 because max_ending_here < 0 |
| 65 | + |
| 66 | +// for i=1, a[1] = -3 |
| 67 | +// max_ending_here = max_ending_here + (-3) |
| 68 | +// Set max_ending_here = 0 because max_ending_here < 0 |
| 69 | + |
| 70 | +// for i=2, a[2] = 4 |
| 71 | +// max_ending_here = max_ending_here + (4) |
| 72 | +// max_ending_here = 4 |
| 73 | +// max_so_far is updated to 4 because max_ending_here greater |
| 74 | +// than max_so_far which was 0 till now |
| 75 | + |
| 76 | +// for i=3, a[3] = -1 |
| 77 | +// max_ending_here = max_ending_here + (-1) |
| 78 | +// max_ending_here = 3 |
| 79 | + |
| 80 | +// for i=4, a[4] = -2 |
| 81 | +// max_ending_here = max_ending_here + (-2) |
| 82 | +// max_ending_here = 1 |
| 83 | + |
| 84 | +// for i=5, a[5] = 1 |
| 85 | +// max_ending_here = max_ending_here + (1) |
| 86 | +// max_ending_here = 2 |
| 87 | + |
| 88 | +// for i=6, a[6] = 5 |
| 89 | +// max_ending_here = max_ending_here + (5) |
| 90 | +// max_ending_here = 7 |
| 91 | +// max_so_far is updated to 7 because max_ending_here is |
| 92 | +// greater than max_so_far |
| 93 | + |
| 94 | +// for i=7, a[7] = -3 |
| 95 | +// max_ending_here = max_ending_here + (-3) |
| 96 | +// max_ending_here = 4 |
| 97 | + |
0 commit comments