-
Notifications
You must be signed in to change notification settings - Fork 19.9k
Fixes(#2877) #2913
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
Closed
Closed
Fixes(#2877) #2913
Changes from 23 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
38daad8
Fixes: #{2391}
siddhant2002 a417229
Fixes #(2391)
siddhant2002 4cd2497
Merge branch 'TheAlgorithms:master' into master
siddhant2002 e3c28dc
Delete String_Comparision.java
siriak 0e97ad8
Merge branch 'master' into master
siriak f27eff4
Fixes: #{2820}
siddhant2002 bdc2a8a
Merge branch 'TheAlgorithms:master' into master
siddhant2002 9e77af5
Fixes #{2820}
siddhant2002 3cacb76
Merge branch 'master' of https://github.com/siddhant2002/Java
siddhant2002 a192de6
Fixes(#2450)
siddhant2002 516c7b9
Merge branch 'TheAlgorithms:master' into master
siddhant2002 10e58e3
string Codes removed
siddhant2002 c62aea7
Merge branch 'master' into master
siddhant2002 d24fdf2
fixes(#2882)
siddhant2002 30615b3
Merge branch 'master' of https://github.com/siddhant2002/Java
siddhant2002 20261bf
Merge branch 'TheAlgorithms:master' into master
siddhant2002 f9fc81e
Fixes(2882)
siddhant2002 86b0102
Merge branch 'master' of https://github.com/siddhant2002/Java
siddhant2002 f5a0937
Merge branch 'TheAlgorithms:master' into master
siddhant2002 a214d7f
Merge branch 'TheAlgorithms:master' into master
siddhant2002 e136a3b
Fixes(#2877)
siddhant2002 e5541ed
Merge branch 'master' of https://github.com/siddhant2002/Java
siddhant2002 6220cee
Fixes(#2877)
siddhant2002 ab73827
Fixes(#2877)
siddhant2002 17009fc
Merge branch 'TheAlgorithms:master' into master
siddhant2002 f903458
Fixes(#2877)
siddhant2002 2467c8c
Merge branch 'master' of https://github.com/siddhant2002/Java
siddhant2002 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
77 changes: 34 additions & 43 deletions
77
src/main/java/com/thealgorithms/dynamicprogramming/KadaneAlgorithm.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,45 @@ | ||
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 sum=a[0],running_sum=0; | ||
for(int k:a) | ||
{ | ||
return 0; | ||
} | ||
for (i = 0; i < len; i++) { | ||
cursum += arr[i]; | ||
if (cursum > maxsum) { | ||
maxsum = cursum; | ||
} | ||
if (cursum <= 0) { | ||
cursum = 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 | ||
} | ||
return maxsum; | ||
// for-each loop is used to iterate over the array and find the maximum subarray sum | ||
return task(a,sum); | ||
// the max_Sum method returns true if the "sub array sum" matches with the "sum of all the elements present in the array" else it returns false | ||
} | ||
|
||
/** | ||
* 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(); | ||
static boolean task(int a[] , int s) | ||
{ | ||
int p=0; | ||
// p is created inorder to store the sum of the elements present in the array | ||
for(int i:a) | ||
{ | ||
p=p+i; | ||
} | ||
int maxContSum = largestContiguousSum(arr); | ||
System.out.println(maxContSum); | ||
sc.close(); | ||
// for-each loop is initialized inorder to get the sum of all the elements of the array | ||
return p==s; | ||
// the task method adds all the elements of the array and checks it with the "maximum subarray sum". If the sum is equal then it returns true else it returns false | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/test/java/com/thealgorithms/others/KadaneAlogrithmTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
@Test | ||
void testForTwoElements() | ||
{ | ||
int a[]={-2,1}; | ||
assertFalse(KadaneAlgorithm.max_Sum(a)); | ||
} | ||
|
||
@Test | ||
void testForThreeElements() | ||
{ | ||
int a[]={5,3,12}; | ||
assertTrue(KadaneAlgorithm.max_Sum(a)); | ||
} | ||
|
||
@Test | ||
void testForFourElements() | ||
{ | ||
int a[]={-1,-3,-7,-4}; | ||
assertFalse(KadaneAlgorithm.max_Sum(a)); | ||
} | ||
|
||
@Test | ||
void testForFiveElements() | ||
{ | ||
int a[]={4,5,3,0,2}; | ||
assertTrue(KadaneAlgorithm.max_Sum(a)); | ||
} | ||
|
||
|
||
@Test | ||
void testForSixElements() | ||
{ | ||
int a[]={-43,-45,47,12,87,-13}; | ||
assertFalse(KadaneAlgorithm.max_Sum(a)); | ||
} | ||
|
||
@Test | ||
void testForSevenElements() | ||
{ | ||
int a[]={9,8,2,23,13,6,7}; | ||
assertTrue(KadaneAlgorithm.max_Sum(a)); | ||
} | ||
|
||
@Test | ||
void testForEightElements() | ||
{ | ||
int a[]={9,-5,-5,-2,4,5,0,1}; | ||
assertFalse(KadaneAlgorithm.max_Sum(a)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does it mean? The purpose of the algorithm is to find the largest possible sum, why does it return bool?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically it's checking with the predicted answer