Skip to content

Add tests, enhance docs in KadaneAlgorithm.java #5646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,7 @@
* [EditDistanceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/EditDistanceTest.java)
* [EggDroppingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/EggDroppingTest.java)
* [FibonacciTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/FibonacciTest.java)
* [KadaneAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/KadaneAlgorithmTest.java)
* [KnapsackMemoizationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/KnapsackMemoizationTest.java)
* [KnapsackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/KnapsackTest.java)
* [LevenshteinDistanceTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LevenshteinDistanceTests.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,52 @@
package com.thealgorithms.dynamicprogramming;

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

/**
* OUTPUT :
* Input - {89,56,98,123,26,75,12,40,39,68,91}
* Output: it returns either true or false
* 1st approach Time Complexity : O(n)
* Auxiliary Space Complexity : O(1)
* Computes the maximum subarray sum using Kadane's Algorithm and checks
* if it matches a predicted answer.
*
* @param a The input array of integers for which the maximum
* subarray sum is to be calculated.
* @param predictedAnswer The expected maximum subarray sum to be verified
* against the computed sum.
* @return true if the computed maximum subarray sum equals the predicted
* answer, false otherwise.
*
* <p>Example:</p>
* <pre>
* Input: {89, 56, 98, 123, 26, 75, 12, 40, 39, 68, 91}
* Output: true if the maximum subarray sum is equal to the
* predicted answer.
* </pre>
*
* <p>Algorithmic Complexity:</p>
* <ul>
* <li>Time Complexity: O(n) - the algorithm iterates through the array once.</li>
* <li>Auxiliary Space Complexity: O(1) - only a constant amount of additional space is used.</li>
* </ul>
*/
public static boolean maxSum(int[] a, int predictedAnswer) {
int sum = a[0];
int runningSum = 0;

for (int k : a) {
runningSum = runningSum + k;
// running sum of all the indexs are stored
runningSum += k;
sum = Math.max(sum, runningSum);
// the max is stored inorder to the get the maximum sum
if (runningSum < 0) {
runningSum = 0;
}
// if running sum is negative then it is initialized to zero
}
// for-each loop is used to iterate over the array and find the maximum subarray sum

return sum == predictedAnswer;
// It returns true if sum and predicted answer matches
// The predicted answer is the answer itself. So it always return true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.thealgorithms.dynamicprogramming;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

public class KadaneAlgorithmTest {

@Test
void testMaxSumWithPositiveValues() {
// Test with all positive numbers
int[] input = {89, 56, 98, 123, 26, 75, 12, 40, 39, 68, 91};
int expectedMaxSum = 89 + 56 + 98 + 123 + 26 + 75 + 12 + 40 + 39 + 68 + 91; // sum of all elements
assertTrue(KadaneAlgorithm.maxSum(input, expectedMaxSum));
}

@Test
void testMaxSumWithMixedValues() {
// Test with mixed positive and negative numbers
int[] input = {1, -2, 3, 4, -1, 2, 1, -5, 4};
int expectedMaxSum = 3 + 4 + -1 + 2 + 1; // max subarray is [3, 4, -1, 2, 1]
assertTrue(KadaneAlgorithm.maxSum(input, expectedMaxSum));
}

@Test
void testMaxSumWithAllNegativeValues() {
// Test with all negative numbers
int[] input = {-2, -3, -1, -4};
int expectedMaxSum = -1; // max subarray is the least negative number
assertTrue(KadaneAlgorithm.maxSum(input, expectedMaxSum));
}

@Test
void testMaxSumWithSingleElement() {
// Test with a single positive element
int[] input = {10};
int expectedMaxSum = 10; // max subarray is the single element
assertTrue(KadaneAlgorithm.maxSum(input, expectedMaxSum));

// Test with a single negative element
input = new int[] {-10};
expectedMaxSum = -10; // max subarray is the single element
assertTrue(KadaneAlgorithm.maxSum(input, expectedMaxSum));
}

@Test
void testMaxSumWithZero() {
// Test with zeros in the array
int[] input = {0, -1, 2, -2, 0, 3};
int expectedMaxSum = 3; // max subarray is [2, -2, 0, 3]
assertTrue(KadaneAlgorithm.maxSum(input, expectedMaxSum));
}

@Test
void testMaxSumWithEmptyArray() {
// Test with an empty array; should ideally throw an exception or return false
int[] input = {};
assertThrows(ArrayIndexOutOfBoundsException.class, () -> { KadaneAlgorithm.maxSum(input, 0); });
}
}