Merge pull request #12296 from anuragkumawat/JAVA-12383

JAVA-12383 GitHub Issue: Incorrect results in Kadane Algorithm for Maximal Subarray
This commit is contained in:
kwoyke 2022-06-02 22:36:10 +02:00 committed by GitHub
commit 673cbbadd8
2 changed files with 13 additions and 1 deletions

View File

@ -14,7 +14,7 @@ public class KadaneAlgorithm {
int end = 0;
int maxSoFar = arr[0], maxEndingHere = arr[0];
for (int i = 0; i < size; i++) {
for (int i = 1; i < size; i++) {
if (arr[i] > maxEndingHere + arr[i]) {
start = i;

View File

@ -27,4 +27,16 @@ class KadaneAlgorithmUnitTest {
//then
assertEquals(-1, maxSum);
}
@Test
void givenArrayWithAllPosiitveNumbersWhenMaximumSubarrayThenReturnsExpectedResult() {
//given
int[] arr = new int[] {4, 1, 3, 2};
//when
KadaneAlgorithm algorithm = new KadaneAlgorithm();
int maxSum = algorithm.maxSubArraySum(arr);
//then
assertEquals(10, maxSum);
}
}