Skip to content
Merged
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
17 changes: 17 additions & 0 deletions Math/kadane's_algorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def max_subarray_sum(nums):
# Initialize variables to keep track of the maximum subarray sum

max_ending_here = nums[0] # Maximum sum ending at the current position
max_so_far = nums[0] # Maximum sum seen so far

# Iterate through the array, starting from the second element
for i in range(1, len(nums)):
# Calculate the maximum sum ending at the current position by considering whether it's better to
# start a new subarray or extend the previous one.
max_ending_here = max(nums[i], max_ending_here + nums[i])

# Update the maximum sum seen so far by comparing it with the maximum sum ending at the current position.
max_so_far = max(max_so_far, max_ending_here)

# The 'max_so_far' variable now contains the maximum subarray sum.
return max_so_far