- Notifications
You must be signed in to change notification settings - Fork25
Kadane's algorithm for maximum sum contiguous subarray#39
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Closed
Uh oh!
There was an error while loading.Please reload this page.
Closed
Changes fromall commits
Commits
Show all changes
16 commits Select commitHold shift + click to select a range
9f853b8 Create kadane.py
kunal768a8cf0a2 Update kadane.py
kunal7680b7cbcf added test cases
kunal7685f09974 Create kadane.md
kunal7682f43454 Update readme.md
kunal76827de68e Update readme.md
kunal76858d5b5b Update readme.md
kunal7685731fd8 Update readme.md
kunal768cb28675 Update kadane.py
kunal76857d5655 Update readme.md
kunal768d1550b5 Create __init__.py
kunal76832730be Update test_kadane.py
kunal768b7f3c53 Update test_kadane.py
kunal7682e04674 Update test_kadane.py
kunal768faae893 Update test_kadane.py
kunal76873ab13e Update test_kadane.py
kunal768File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionsallalgorithms/subarray/__init__.py
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 @@ | ||
| from .kadane import * |
44 changes: 44 additions & 0 deletionsallalgorithms/subarray/kadane.py
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,44 @@ | ||
| # -*- coding: UTF-8 -*- | ||
| # | ||
| # Kadane's Algorithm | ||
| # The All ▲lgorithms library for python | ||
| # | ||
| # Contributed by: Kunal Keshav Singh Sahni | ||
| # Github: @kunal768 | ||
| # | ||
| import sys | ||
| def maxsum_subarray(arr): | ||
| curr = arr[0] | ||
| maxx = arr[0] | ||
| n = len(arr) | ||
| for i in range(1,n): | ||
| curr = max(arr[i],curr+arr[i]) | ||
| maxx = max(curr,maxx) | ||
| return maxx | ||
| def returnArray(arr): | ||
| size = len(arr) | ||
| max_so_far = -sys.maxsize - 1 | ||
| max_ending_here = 0 | ||
| start = 0 | ||
| end = 0 | ||
| s = 0 | ||
| for i in range(0,size): | ||
| max_ending_here += arr[i] | ||
| if max_so_far < max_ending_here: | ||
| max_so_far = max_ending_here | ||
| start = s | ||
| end = i | ||
| if max_ending_here < 0: | ||
| max_ending_here = 0 | ||
| s = i+1 | ||
| return [max_so_far,arr[start:end+1]] |
7 changes: 6 additions & 1 deletiondocs/readme.md
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
36 changes: 36 additions & 0 deletionsdocs/subarray/kadane.md
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,36 @@ | ||
| # Kadane's Algorithm | ||
| Kadane’s algorithm is to look for all positive contiguous segments of the array (max_ending_here is used for this). And keep track of maximum sum contiguous segment among all positive segments (max_so_far is used for this) Each time we get a positive sum compare it with max_so_far and update max_so_far if it is greater than max_so_far | ||
| ## Install | ||
| ``` | ||
| pip install allalgorithms | ||
| ``` | ||
| ## Usage | ||
| ```py | ||
| from allalgorithms.subarray import kadane | ||
| arr = [-2, 1, 2, 7, 10, 77] | ||
| print(kadane.maxsum_subarray(arr)) | ||
| # -> 97 | ||
| print(kadane.returnArray(arr)) | ||
| # -> [97, [1, 2, 7, 10, 77]] | ||
| ``` | ||
| ## API | ||
| ### maxsum_subarray(array) | ||
| > Return sum of maximum contigious subarray | ||
| ### returnArray(array) | ||
| > Return sum along with the respective subarray | ||
| ##### Params: | ||
| - `array`: Array (may contain negative elements) |
5 changes: 5 additions & 0 deletionsreadme.md
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
19 changes: 19 additions & 0 deletionstests/test_kadane.py
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,19 @@ | ||
| import unittest | ||
| from allalgorithms.subarray import kadane | ||
| class TestSearches(unittest.TestCase): | ||
| def test_returnArray(self): | ||
| self.assertEqual( [11,[2,3,-1,7]], kadane.returnArray([2,3,-1,7])) | ||
| self.assertEqual([5,[2,3]], kadane.returnArray([2,3,-2,4])) | ||
| self.assertEqual([0, [0]], kadane.returnArray([-1,-1,-0,0])) | ||
| self.assertEqual([-1, [-1]], kadane.returnArray([-1])) | ||
| def test_maxsum_subarray(self): | ||
| self.assertEqual(11,kadane.maxsum_subarray([2,3,-1,7])) | ||
| self.assertEqual(5, kadane.maxsum_subarray([2,3,-2,4])) | ||
| self.assertEqual(0, kadane.maxsum_subarray([-1,-1,-0,0])) | ||
| self.assertEqual(-1, kadane.maxsum_subarray([-1])) | ||
| if __name__ == '__main__': | ||
| unittest.main() |
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.