mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 00:54:32 +08:00
Co-authored-by: Andrii Siriak <siryaka@gmail.com>
This commit is contained in:

committed by
GitHub

parent
adadb2f6d6
commit
0a062d4616
@ -1,54 +1,34 @@
|
||||
package com.thealgorithms.dynamicprogramming;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Program to implement Kadane’s Algorithm to calculate maximum contiguous
|
||||
* subarray sum of an array Time Complexity: O(n)
|
||||
*
|
||||
* @author Nishita Aggarwal
|
||||
/** Author : Siddhant Swarup Mallick
|
||||
* Github : https://github.com/siddhant2002
|
||||
*/
|
||||
|
||||
/** Program description - To find the maximum subarray sum */
|
||||
package com.thealgorithms.dynamicprogramming;
|
||||
|
||||
public class KadaneAlgorithm {
|
||||
|
||||
/**
|
||||
* This method implements Kadane's Algorithm
|
||||
*
|
||||
* @param arr The input array
|
||||
* @return The maximum contiguous subarray sum of the array
|
||||
*/
|
||||
static int largestContiguousSum(int arr[]) {
|
||||
int i, len = arr.length, cursum = 0, maxsum = Integer.MIN_VALUE;
|
||||
if (len == 0) // empty array
|
||||
public static boolean max_Sum(int a[] , int predicted_answer)
|
||||
{
|
||||
int sum=a[0],running_sum=0;
|
||||
for(int k:a)
|
||||
{
|
||||
return 0;
|
||||
running_sum=running_sum+k;
|
||||
// running sum of all the indexs are stored
|
||||
sum=Math.max(sum,running_sum);
|
||||
// the max is stored inorder to the get the maximum sum
|
||||
if(running_sum<0)
|
||||
running_sum=0;
|
||||
// if running sum is negative then it is initialized to zero
|
||||
}
|
||||
for (i = 0; i < len; i++) {
|
||||
cursum += arr[i];
|
||||
if (cursum > maxsum) {
|
||||
maxsum = cursum;
|
||||
}
|
||||
if (cursum <= 0) {
|
||||
cursum = 0;
|
||||
}
|
||||
}
|
||||
return maxsum;
|
||||
// for-each loop is used to iterate over the array and find the maximum subarray sum
|
||||
return sum==predicted_answer;
|
||||
// It returns true if sum and predicted answer matches
|
||||
// The predicted answer is the answer itself. So it always return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Main method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
* 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)
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int n, arr[], i;
|
||||
n = sc.nextInt();
|
||||
arr = new int[n];
|
||||
for (i = 0; i < n; i++) {
|
||||
arr[i] = sc.nextInt();
|
||||
}
|
||||
int maxContSum = largestContiguousSum(arr);
|
||||
System.out.println(maxContSum);
|
||||
sc.close();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package com.thealgorithms.others;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import com.thealgorithms.dynamicprogramming.KadaneAlgorithm;
|
||||
public class KadaneAlogrithmTest {
|
||||
@Test
|
||||
void testForOneElement()
|
||||
{
|
||||
int a[]={-1};
|
||||
assertTrue(KadaneAlgorithm.max_Sum(a,-1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForTwoElements()
|
||||
{
|
||||
int a[]={-2,1};
|
||||
assertTrue(KadaneAlgorithm.max_Sum(a,1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForThreeElements()
|
||||
{
|
||||
int a[]={5,3,12};
|
||||
assertTrue(KadaneAlgorithm.max_Sum(a,20));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForFourElements()
|
||||
{
|
||||
int a[]={-1,-3,-7,-4};
|
||||
assertTrue(KadaneAlgorithm.max_Sum(a,-1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForFiveElements()
|
||||
{
|
||||
int a[]={4,5,3,0,2};
|
||||
assertTrue(KadaneAlgorithm.max_Sum(a,14));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testForSixElements()
|
||||
{
|
||||
int a[]={-43,-45,47,12,87,-13};
|
||||
assertTrue(KadaneAlgorithm.max_Sum(a,146));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForSevenElements()
|
||||
{
|
||||
int a[]={9,8,2,23,13,6,7};
|
||||
assertTrue(KadaneAlgorithm.max_Sum(a,68));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForEightElements()
|
||||
{
|
||||
int a[]={9,-5,-5,-2,4,5,0,1};
|
||||
assertTrue(KadaneAlgorithm.max_Sum(a,10));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user