Skip to content

Commit e568699

Browse files
Update kadanes.java
1 parent d55b894 commit e568699

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

Arrays/kadanes.java

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
// Java program for Kadane's Algorithms
2-
//
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+
310
class algo{
411

512
static void Kadanes(int a[], int size)
@@ -10,9 +17,11 @@ static void Kadanes(int a[], int size)
1017

1118
for (int i = 0; i < size; i++)
1219
{
20+
// loop to iterate and find the max_ending_here.
1321
max_ending_here += a[i];
1422

1523
if (max_so_far < max_ending_here)
24+
//condition to find the start and end indexes of the sub-array
1625
{
1726
max_so_far = max_ending_here;
1827
start = s;
@@ -25,6 +34,7 @@ static void Kadanes(int a[], int size)
2534
s = i + 1;
2635
}
2736
}
37+
// PRINT THE MAXIMUM CONTIGUOUS SUM WITH THE STARTING AND ENDING INDEXES.
2838
System.out.println("Maximum contiguous sum is "
2939
+ max_so_far);
3040
System.out.println("Starting index " + start);
@@ -36,7 +46,52 @@ public static void main(String[] args)
3646
{
3747
int a[] = { 10, 16, -2, 8, 5, 9, -7, 3 };
3848
int n = a.length;
49+
// CALLING OUR FUNCTION TO IMPLEMENT THE ALGORITH ON THE ABOVE ARRAY a[]
3950
Kadanes(a, n);
4051
}
4152
}
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
4297

0 commit comments

Comments
 (0)