-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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 1 commit
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,69 @@ | ||
""" | ||
Kadane's algorithm to get maximum subarray sum | ||
Please enter the decimal values only and use spaces as seperator | ||
""" | ||
def negative_exist(arr): | ||
|
||
""" | ||
checks wether the max value in -ve or not | ||
eg :- | ||
arr = [-2,-8,-9] | ||
so max element is negative i.e. -2 | ||
so answer is -2 | ||
|
||
and if max value is positive it will return 0 | ||
and then apply the kadane's algorithm | ||
eg :- | ||
arr = [2,8,-9] | ||
positive number exist so it will return 0 | ||
Raj-Parekh24 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_implementation(arr): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Python type hints as discussed in CONTRIBUTING.md? |
||
|
||
|
||
""" | ||
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 th output for above arr is :- 8 | ||
Raj-Parekh24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
if(negative_exist(arr)<0): | ||
Raj-Parekh24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 = list(map(int, input().split())) | ||
Raj-Parekh24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print(kadanes_implementation(arr)) | ||
except ValueError: | ||
print("Please,enter decimal values") | ||
|
||
Raj-Parekh24 marked this conversation as resolved.
Show resolved
Hide resolved
|
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.