Skip to content

Commit 0a062d4

Browse files
siddhant2002siriak
andauthored
Add tests for Kadane algorithm (TheAlgorithms#2877) (TheAlgorithms#2939)
Co-authored-by: Andrii Siriak <[email protected]>
1 parent adadb2f commit 0a062d4

File tree

2 files changed

+89
-46
lines changed

2 files changed

+89
-46
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,34 @@
1-
package com.thealgorithms.dynamicprogramming;
1+
/** Author : Siddhant Swarup Mallick
2+
* Github : https://github.com/siddhant2002
3+
*/
24

3-
import java.util.Scanner;
5+
/** Program description - To find the maximum subarray sum */
6+
package com.thealgorithms.dynamicprogramming;
47

5-
/**
6-
* Program to implement Kadane’s Algorithm to calculate maximum contiguous
7-
* subarray sum of an array Time Complexity: O(n)
8-
*
9-
* @author Nishita Aggarwal
10-
*/
118
public class KadaneAlgorithm {
12-
13-
/**
14-
* This method implements Kadane's Algorithm
15-
*
16-
* @param arr The input array
17-
* @return The maximum contiguous subarray sum of the array
18-
*/
19-
static int largestContiguousSum(int arr[]) {
20-
int i, len = arr.length, cursum = 0, maxsum = Integer.MIN_VALUE;
21-
if (len == 0) // empty array
9+
public static boolean max_Sum(int a[] , int predicted_answer)
10+
{
11+
int sum=a[0],running_sum=0;
12+
for(int k:a)
2213
{
23-
return 0;
14+
running_sum=running_sum+k;
15+
// running sum of all the indexs are stored
16+
sum=Math.max(sum,running_sum);
17+
// the max is stored inorder to the get the maximum sum
18+
if(running_sum<0)
19+
running_sum=0;
20+
// if running sum is negative then it is initialized to zero
2421
}
25-
for (i = 0; i < len; i++) {
26-
cursum += arr[i];
27-
if (cursum > maxsum) {
28-
maxsum = cursum;
29-
}
30-
if (cursum <= 0) {
31-
cursum = 0;
32-
}
33-
}
34-
return maxsum;
22+
// for-each loop is used to iterate over the array and find the maximum subarray sum
23+
return sum==predicted_answer;
24+
// It returns true if sum and predicted answer matches
25+
// The predicted answer is the answer itself. So it always return true
3526
}
36-
3727
/**
38-
* Main method
39-
*
40-
* @param args Command line arguments
28+
* OUTPUT :
29+
* Input - {89,56,98,123,26,75,12,40,39,68,91}
30+
* Output: it returns either true or false
31+
* 1st approach Time Complexity : O(n)
32+
* Auxiliary Space Complexity : O(1)
4133
*/
42-
public static void main(String[] args) {
43-
Scanner sc = new Scanner(System.in);
44-
int n, arr[], i;
45-
n = sc.nextInt();
46-
arr = new int[n];
47-
for (i = 0; i < n; i++) {
48-
arr[i] = sc.nextInt();
49-
}
50-
int maxContSum = largestContiguousSum(arr);
51-
System.out.println(maxContSum);
52-
sc.close();
53-
}
54-
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.thealgorithms.others;
2+
import org.junit.jupiter.api.Test;
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import com.thealgorithms.dynamicprogramming.KadaneAlgorithm;
6+
public class KadaneAlogrithmTest {
7+
@Test
8+
void testForOneElement()
9+
{
10+
int a[]={-1};
11+
assertTrue(KadaneAlgorithm.max_Sum(a,-1));
12+
}
13+
14+
@Test
15+
void testForTwoElements()
16+
{
17+
int a[]={-2,1};
18+
assertTrue(KadaneAlgorithm.max_Sum(a,1));
19+
}
20+
21+
@Test
22+
void testForThreeElements()
23+
{
24+
int a[]={5,3,12};
25+
assertTrue(KadaneAlgorithm.max_Sum(a,20));
26+
}
27+
28+
@Test
29+
void testForFourElements()
30+
{
31+
int a[]={-1,-3,-7,-4};
32+
assertTrue(KadaneAlgorithm.max_Sum(a,-1));
33+
}
34+
35+
@Test
36+
void testForFiveElements()
37+
{
38+
int a[]={4,5,3,0,2};
39+
assertTrue(KadaneAlgorithm.max_Sum(a,14));
40+
}
41+
42+
43+
@Test
44+
void testForSixElements()
45+
{
46+
int a[]={-43,-45,47,12,87,-13};
47+
assertTrue(KadaneAlgorithm.max_Sum(a,146));
48+
}
49+
50+
@Test
51+
void testForSevenElements()
52+
{
53+
int a[]={9,8,2,23,13,6,7};
54+
assertTrue(KadaneAlgorithm.max_Sum(a,68));
55+
}
56+
57+
@Test
58+
void testForEightElements()
59+
{
60+
int a[]={9,-5,-5,-2,4,5,0,1};
61+
assertTrue(KadaneAlgorithm.max_Sum(a,10));
62+
}
63+
}

0 commit comments

Comments
 (0)