Skip to content
Closed
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
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
* [Find Triplets With 0 Sum](data_structures/arrays/find_triplets_with_0_sum.py)
* [Index 2D Array In 1D](data_structures/arrays/index_2d_array_in_1d.py)
* [Kth Largest Element](data_structures/arrays/kth_largest_element.py)
* [Maximum Subarray Sum](data_structures/arrays/maximum_subarray_sum.py)
* [Median Two Array](data_structures/arrays/median_two_array.py)
* [Monotonic Array](data_structures/arrays/monotonic_array.py)
* [Pairs With Given Sum](data_structures/arrays/pairs_with_given_sum.py)
Expand Down
34 changes: 34 additions & 0 deletions data_structures/arrays/maximum_subarray_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
def max_subarray_sum(arr: list[int]) -> int:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/arrays/maximum_subarray_sum.py, please provide doctest for the function max_subarray_sum

"""
Find the maximum sum of a subarray.

Args:
arr: array of numbers.

Returns:
Maximum sum possible in a subarray

Examples:
>>> max_subarray_sum([1, 3, 2])
6

>>> max_subarray_sum([1, 2, 3, -1, 0])
6
"""
ans = arr[0]

for i in range(len(arr)):
current_sum = 0

for j in range(i, len(arr)):
current_sum = current_sum + arr[j]
ans = max(ans, current_sum)

return ans


if __name__ == "__main__":
print(max_subarray_sum([1, 2, 3, 4, 5]))
import doctest

doctest.testmod()