Skip to content

Commit 029dd85

Browse files
authored
Add tests, enhance docs in KadaneAlgorithm.java (#5646)
1 parent ce9f420 commit 029dd85

File tree

3 files changed

+92
-14
lines changed

3 files changed

+92
-14
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,7 @@
792792
* [EditDistanceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/EditDistanceTest.java)
793793
* [EggDroppingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/EggDroppingTest.java)
794794
* [FibonacciTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/FibonacciTest.java)
795+
* [KadaneAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/KadaneAlgorithmTest.java)
795796
* [KnapsackMemoizationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/KnapsackMemoizationTest.java)
796797
* [KnapsackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/KnapsackTest.java)
797798
* [LevenshteinDistanceTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LevenshteinDistanceTests.java)
Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,52 @@
11
package com.thealgorithms.dynamicprogramming;
22

33
/**
4-
* @author <a href="https://github.com/siddhant2002">Siddhant Swarup Mallick</a>
5-
* Program description - To find the maximum subarray sum
4+
* This class implements Kadane's Algorithm to find the maximum subarray sum
5+
* within a given array of integers. The algorithm efficiently computes the maximum
6+
* sum of a contiguous subarray in linear time.
7+
*
8+
* Author: <a href="https://github.com/siddhant2002">Siddhant Swarup Mallick</a>
69
*/
710
public final class KadaneAlgorithm {
811
private KadaneAlgorithm() {
912
}
1013

1114
/**
12-
* OUTPUT :
13-
* Input - {89,56,98,123,26,75,12,40,39,68,91}
14-
* Output: it returns either true or false
15-
* 1st approach Time Complexity : O(n)
16-
* Auxiliary Space Complexity : O(1)
15+
* Computes the maximum subarray sum using Kadane's Algorithm and checks
16+
* if it matches a predicted answer.
17+
*
18+
* @param a The input array of integers for which the maximum
19+
* subarray sum is to be calculated.
20+
* @param predictedAnswer The expected maximum subarray sum to be verified
21+
* against the computed sum.
22+
* @return true if the computed maximum subarray sum equals the predicted
23+
* answer, false otherwise.
24+
*
25+
* <p>Example:</p>
26+
* <pre>
27+
* Input: {89, 56, 98, 123, 26, 75, 12, 40, 39, 68, 91}
28+
* Output: true if the maximum subarray sum is equal to the
29+
* predicted answer.
30+
* </pre>
31+
*
32+
* <p>Algorithmic Complexity:</p>
33+
* <ul>
34+
* <li>Time Complexity: O(n) - the algorithm iterates through the array once.</li>
35+
* <li>Auxiliary Space Complexity: O(1) - only a constant amount of additional space is used.</li>
36+
* </ul>
1737
*/
1838
public static boolean maxSum(int[] a, int predictedAnswer) {
1939
int sum = a[0];
2040
int runningSum = 0;
41+
2142
for (int k : a) {
22-
runningSum = runningSum + k;
23-
// running sum of all the indexs are stored
43+
runningSum += k;
2444
sum = Math.max(sum, runningSum);
25-
// the max is stored inorder to the get the maximum sum
2645
if (runningSum < 0) {
2746
runningSum = 0;
2847
}
29-
// if running sum is negative then it is initialized to zero
3048
}
31-
// for-each loop is used to iterate over the array and find the maximum subarray sum
49+
3250
return sum == predictedAnswer;
33-
// It returns true if sum and predicted answer matches
34-
// The predicted answer is the answer itself. So it always return true
3551
}
3652
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
import static org.junit.jupiter.api.Assertions.assertThrows;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
public class KadaneAlgorithmTest {
9+
10+
@Test
11+
void testMaxSumWithPositiveValues() {
12+
// Test with all positive numbers
13+
int[] input = {89, 56, 98, 123, 26, 75, 12, 40, 39, 68, 91};
14+
int expectedMaxSum = 89 + 56 + 98 + 123 + 26 + 75 + 12 + 40 + 39 + 68 + 91; // sum of all elements
15+
assertTrue(KadaneAlgorithm.maxSum(input, expectedMaxSum));
16+
}
17+
18+
@Test
19+
void testMaxSumWithMixedValues() {
20+
// Test with mixed positive and negative numbers
21+
int[] input = {1, -2, 3, 4, -1, 2, 1, -5, 4};
22+
int expectedMaxSum = 3 + 4 + -1 + 2 + 1; // max subarray is [3, 4, -1, 2, 1]
23+
assertTrue(KadaneAlgorithm.maxSum(input, expectedMaxSum));
24+
}
25+
26+
@Test
27+
void testMaxSumWithAllNegativeValues() {
28+
// Test with all negative numbers
29+
int[] input = {-2, -3, -1, -4};
30+
int expectedMaxSum = -1; // max subarray is the least negative number
31+
assertTrue(KadaneAlgorithm.maxSum(input, expectedMaxSum));
32+
}
33+
34+
@Test
35+
void testMaxSumWithSingleElement() {
36+
// Test with a single positive element
37+
int[] input = {10};
38+
int expectedMaxSum = 10; // max subarray is the single element
39+
assertTrue(KadaneAlgorithm.maxSum(input, expectedMaxSum));
40+
41+
// Test with a single negative element
42+
input = new int[] {-10};
43+
expectedMaxSum = -10; // max subarray is the single element
44+
assertTrue(KadaneAlgorithm.maxSum(input, expectedMaxSum));
45+
}
46+
47+
@Test
48+
void testMaxSumWithZero() {
49+
// Test with zeros in the array
50+
int[] input = {0, -1, 2, -2, 0, 3};
51+
int expectedMaxSum = 3; // max subarray is [2, -2, 0, 3]
52+
assertTrue(KadaneAlgorithm.maxSum(input, expectedMaxSum));
53+
}
54+
55+
@Test
56+
void testMaxSumWithEmptyArray() {
57+
// Test with an empty array; should ideally throw an exception or return false
58+
int[] input = {};
59+
assertThrows(ArrayIndexOutOfBoundsException.class, () -> { KadaneAlgorithm.maxSum(input, 0); });
60+
}
61+
}

0 commit comments

Comments
 (0)