Skip to content

Added Kadane's Algorithm for Maximum Subarray Sum #11792

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

Closed
wants to merge 3 commits into from
Closed
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
71 changes: 71 additions & 0 deletions data_structures/arrays/maximum_subarray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""
Kadane's Algorithm is an efficient method to find the maximum
sum of a contiguous subarray within a one-dimensional array of
numbers. It maintains two key values as we traverse the array:
the current maximum sum ending at the current index and the
global maximum sum found so far.
"""

# Advantages
"""
- Efficiency: Runs in linear time, `O(n)`.
- Simplicity: Straightforward to implement and understand.
- Versatility: Easily adaptable to related problems.
"""

# Time Complexity
"""
- Time Complexity: `O(n)` - processes each element once.
- Space Complexity: `O(1)` - uses a fixed amount of extra space.
"""

"""
Find the Maximum Subarray Sum using Kadane's Algorithm.
Reference: https://leetcode.com/problems/maximum-subarray/

Python doctest can be run with the following command:
python -m doctest -v maximum_subarray.py

Given an integer array nums, this function returns
the maximum sum of a contiguous subarray.

A subarray is a contiguous part of an array.

Example Input:
nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
Output: 6
"""

def max_subarray_sum(nums: list[int]) -> int:
"""
Find the maximum subarray sum using Kadane's Algorithm.

Args:
nums (list[int]): The input array of integers.

Returns:
int: The maximum subarray sum.

Examples:
>>> max_subarray_sum([-2, 1, -3, 4, -1, 2, 1, -5, 4])
6
>>> max_subarray_sum([1])
1
>>> max_subarray_sum([5, 4, -1, 7, 8])
23
>>> max_subarray_sum([-1, -2, -3, -4])
-1
"""
max_current = max_global = nums[0]

for num in nums[1:]:
max_current = max(num, max_current + num)
max_global = max(max_global, max_current)

return max_global


if __name__ == "__main__":
import doctest

doctest.testmod()

Check failure on line 71 in data_structures/arrays/maximum_subarray.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W292)

data_structures/arrays/maximum_subarray.py:71:22: W292 No newline at end of file

Check failure on line 71 in data_structures/arrays/maximum_subarray.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W292)

data_structures/arrays/maximum_subarray.py:71:22: W292 No newline at end of file

Check failure on line 71 in data_structures/arrays/maximum_subarray.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W292)

data_structures/arrays/maximum_subarray.py:71:22: W292 No newline at end of file
Loading