Skip to content

Fixes(#2877) #2939

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 29 commits into from
Feb 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
38daad8
Fixes: #{2391}
siddhant2002 Dec 5, 2021
a417229
Fixes #(2391)
siddhant2002 Dec 7, 2021
4cd2497
Merge branch 'TheAlgorithms:master' into master
siddhant2002 Dec 8, 2021
e3c28dc
Delete String_Comparision.java
siriak Dec 9, 2021
0e97ad8
Merge branch 'master' into master
siriak Dec 9, 2021
f27eff4
Fixes: #{2820}
siddhant2002 Dec 14, 2021
bdc2a8a
Merge branch 'TheAlgorithms:master' into master
siddhant2002 Dec 14, 2021
9e77af5
Fixes #{2820}
siddhant2002 Dec 14, 2021
3cacb76
Merge branch 'master' of https://github.com/siddhant2002/Java
siddhant2002 Dec 14, 2021
a192de6
Fixes(#2450)
siddhant2002 Dec 20, 2021
516c7b9
Merge branch 'TheAlgorithms:master' into master
siddhant2002 Dec 20, 2021
10e58e3
string Codes removed
siddhant2002 Dec 21, 2021
c62aea7
Merge branch 'master' into master
siddhant2002 Dec 22, 2021
d24fdf2
fixes(#2882)
siddhant2002 Dec 22, 2021
30615b3
Merge branch 'master' of https://github.com/siddhant2002/Java
siddhant2002 Dec 22, 2021
20261bf
Merge branch 'TheAlgorithms:master' into master
siddhant2002 Jan 18, 2022
f9fc81e
Fixes(2882)
siddhant2002 Jan 19, 2022
86b0102
Merge branch 'master' of https://github.com/siddhant2002/Java
siddhant2002 Jan 19, 2022
f5a0937
Merge branch 'TheAlgorithms:master' into master
siddhant2002 Jan 20, 2022
a214d7f
Merge branch 'TheAlgorithms:master' into master
siddhant2002 Jan 22, 2022
e136a3b
Fixes(#2877)
siddhant2002 Jan 22, 2022
e5541ed
Merge branch 'master' of https://github.com/siddhant2002/Java
siddhant2002 Jan 22, 2022
6220cee
Fixes(#2877)
siddhant2002 Feb 11, 2022
ab73827
Fixes(#2877)
siddhant2002 Feb 12, 2022
17009fc
Merge branch 'TheAlgorithms:master' into master
siddhant2002 Feb 12, 2022
f903458
Fixes(#2877)
siddhant2002 Feb 12, 2022
2467c8c
Merge branch 'master' of https://github.com/siddhant2002/Java
siddhant2002 Feb 12, 2022
ababfa4
Fixes(#2877)
siddhant2002 Feb 13, 2022
255e4df
Fixes(#2877)
siddhant2002 Feb 13, 2022
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
Original file line number Diff line number Diff line change
@@ -1,54 +1,34 @@
package com.thealgorithms.dynamicprogramming;
/** Author : Siddhant Swarup Mallick
* Github : https://github.com/siddhant2002
*/

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

/**
* Program to implement Kadane’s Algorithm to calculate maximum contiguous
* subarray sum of an array Time Complexity: O(n)
*
* @author Nishita Aggarwal
*/
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();
}
}
}
63 changes: 63 additions & 0 deletions src/test/java/com/thealgorithms/others/KadaneAlogrithmTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}