Skip to content
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

Add category divide_and_conquer #167

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
37 changes: 37 additions & 0 deletions Divide_And_Conquer/max_subarray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
def find_max_crossing_subarray(arr, low, mid, high):
left_sum = -1
sum = 0
for i in range(mid, 0, -1):
sum += arr[i]
if sum > left_sum:
left_sum = sum

right_sum = -1
sum = 0
for j in range(mid+1, high + 1):
sum += arr[j]
if sum > right_sum:
right_sum = sum

return (left_sum + right_sum)

def find_max_subarray(arr, low, high):

if high == low:
return arr[low]

else:
mid = (low + high) // 2
left_sum = find_max_subarray(arr, low, mid)
right_sum = find_max_subarray(arr, mid+1, high)
cross_sum = find_max_crossing_subarray(arr, low, mid, high)

if left_sum >= right_sum and left_sum >= cross_sum:
return left_sum
elif right_sum >= left_sum and right_sum >= cross_sum:
return right_sum
else:
return cross_sum

arr = [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7]
print(find_max_subarray(arr, 0, len(arr) - 1))
4 changes: 1 addition & 3 deletions PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
```
# Description

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. Include a proper title of the Script that is being entered.
Expand All @@ -12,5 +11,4 @@ Please delete options that are not relevant.
- [ ] New Script
- [ ] New Category (Is any new category being made to accommodate this script)
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
```
- [ ] New feature (non-breaking change which adds functionality)