-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Kadanes_algorithm #1959
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
Kadanes_algorithm #1959
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
23e85f6
Add files via upload
Raj-Parekh24 56e1222
Update Kadane's_algorithm.py
Raj-Parekh24 244325c
Update and rename Kadane's_algorithm.py to kadanes_algorithm.py
Raj-Parekh24 62a5b2e
Update kadanes_algorithm.py
Raj-Parekh24 5aa0304
Update kadanes_algorithm.py
Raj-Parekh24 8c9c8da
Update kadanes_algorithm.py
Raj-Parekh24 7b0d50c
Update kadanes_algorithm.py
Raj-Parekh24 ca2a480
Update kadanes_algorithm.py
cclauss 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
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,61 @@ | ||
""" | ||
Link for the explanation of this algorithm :- | ||
https://medium.com/@rsinghal757/kadanes-algorithm-dynamic-programming-how-and-why-does-it-work-3fd8849ed73d | ||
Kadane's algorithm to get maximum subarray sum | ||
Please enter the decimal values only and use spaces as separator | ||
""" | ||
def negative_exist(arr): | ||
|
||
""" | ||
>>> negative_exist([-2,-8,-9]) | ||
-2 | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
max=arr[0] | ||
for i in arr: | ||
if(i>=0): | ||
return 0; | ||
elif(max<=i): | ||
max=i | ||
return max | ||
|
||
|
||
def kadanes(arr): | ||
|
||
|
||
""" | ||
if negative_exist return 0 than this function will execute | ||
else it will return the value return by negative_exist function | ||
|
||
Eg :- | ||
arr = [2,3,-9,8,-2] | ||
initially the value of max_sum = 0 and max_till_element is 0 | ||
than when max_sum less than max_till particular element it will assign that value to max_sum | ||
and when value of max_till_sum is less than 0 it will assign 0 to i | ||
and after whole process it will return the value | ||
|
||
So the output for above arr is :- 8 | ||
|
||
>>> kadanes([2,3,-9,8,-2]) | ||
8 | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
if negative_exist(arr) < 0: | ||
return negative_exist(arr) | ||
|
||
max_sum = 0 | ||
max_till_element=0 | ||
|
||
for i in arr: | ||
max_till_element=max_till_element+i | ||
if max_sum <= max_till_element: | ||
max_sum = max_till_element | ||
if max_till_element < 0: | ||
max_till_element = 0 | ||
return max_sum | ||
|
||
if __name__ == "__main__": | ||
try: | ||
print("Enter the element of array with spaces :- ") | ||
arr = [int(x) for x in input().split()] | ||
print(f"Maximum subarray sum of {arr} is {kadanes(arr)}") | ||
except ValueError: | ||
print("Please,enter decimal values") |
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.
Uh oh!
There was an error while loading. Please reload this page.