diff --git a/algorithms/Python/README.md b/algorithms/Python/README.md index 9bcc929b..a2949840 100644 --- a/algorithms/Python/README.md +++ b/algorithms/Python/README.md @@ -7,6 +7,7 @@ - [Missing Number](arrays/missing_number.py) - [Remove duplicate items](arrays/remove_duplicates_list.py) - [Dutch National Flag Algorithm](arrays/dutch_national_flag_algo.py) +- [Max Sub Array Sum](arrays/max_sub_array_sum.py) ## Linked Lists - [Doubly](linked_lists/doubly.py) diff --git a/algorithms/Python/arrays/max_sub_array_sum.py b/algorithms/Python/arrays/max_sub_array_sum.py new file mode 100644 index 00000000..96f0e1dd --- /dev/null +++ b/algorithms/Python/arrays/max_sub_array_sum.py @@ -0,0 +1,32 @@ +""" +Algorithm Name: Max Sum of Sub Array +Time Complexity: O(n) +Explanation: arr = [3, 2, -4, 9] +at the start of the algorithm +assign current sum (max_sum_curr) = max sum(max_sum) = arr[0] +(for) iterate from arr[1] to arr[n] and do + max_sum_curr = arr[i] if arr[i] > arr[i] + max_sum_curr + else + max_sum_curr = max_sum_curr + arr[i] + max_sum = max_sum if max_sum > max_sum_curr + else + max_sum = max_sum_curr +end +return max_sum +""" + +def max_sub_arr_sum(arr): + arr_size = len(arr) + max_sum = arr[0] + max_sum_curr = arr[0] + + for i in range(1, arr_size): + max_sum_curr = max(arr[i], max_sum_curr + arr[i]) + max_sum = max(max_sum, max_sum_curr) + + return max_sum + + +# print("Enter array of numbers (Ex: 1 2 3 4 for [1, 2, 3, 4])") +arr = [3, 2, -4, 9] # list(map(int, input().split())) +print("Maximum Sub Array Sum is", max_sub_arr_sum(arr))