You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Arrays/kadanes.java
+56-1Lines changed: 56 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,12 @@
1
1
// 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
+
3
10
classalgo{
4
11
5
12
staticvoidKadanes(inta[], intsize)
@@ -10,9 +17,11 @@ static void Kadanes(int a[], int size)
10
17
11
18
for (inti = 0; i < size; i++)
12
19
{
20
+
// loop to iterate and find the max_ending_here.
13
21
max_ending_here += a[i];
14
22
15
23
if (max_so_far < max_ending_here)
24
+
//condition to find the start and end indexes of the sub-array
16
25
{
17
26
max_so_far = max_ending_here;
18
27
start = s;
@@ -25,6 +34,7 @@ static void Kadanes(int a[], int size)
25
34
s = i + 1;
26
35
}
27
36
}
37
+
// PRINT THE MAXIMUM CONTIGUOUS SUM WITH THE STARTING AND ENDING INDEXES.
28
38
System.out.println("Maximum contiguous sum is "
29
39
+ max_so_far);
30
40
System.out.println("Starting index " + start);
@@ -36,7 +46,52 @@ public static void main(String[] args)
36
46
{
37
47
inta[] = { 10, 16, -2, 8, 5, 9, -7, 3 };
38
48
intn = a.length;
49
+
// CALLING OUR FUNCTION TO IMPLEMENT THE ALGORITH ON THE ABOVE ARRAY a[]
39
50
Kadanes(a, n);
40
51
}
41
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
0 commit comments