Skip to content

Commit 96fcb53

Browse files
Update maximum_subarray_sum.py
1 parent 5971ffe commit 96fcb53

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1-
def maxSubarraySum(arr):
1+
def maxSubarraySum(arr: list[int) -> int:
2+
"""
3+
Find the maximum sum of a subarray.
4+
5+
Args:
6+
arr: array of numbers.
7+
8+
Returns:
9+
Maximum sum possible in a subarray
10+
"""
211
ans = arr[0]
312

413
for i in range(len(arr)):
5-
currentSum = 0
14+
current_sum = 0
615

716
for j in range(i, len(arr)):
8-
currentSum = currentSum + arr[j]
9-
ans = max(ans, currentSum)
17+
current_sum = current_sum + arr[j]
18+
ans = max(ans, current_sum)
1019

1120
return ans
1221

1322

1423
if __name__ == "__main__":
1524
arr = list(map(int, input().split(" ")))
1625
print(maxSubarraySum(arr))
26+
import doctest
27+
doctest.testmod()

0 commit comments

Comments
 (0)