From efa5b8ef3f1fd2b9cf8ec7cd1e7e1850d86aa0d5 Mon Sep 17 00:00:00 2001 From: Saurav Date: Thu, 9 Apr 2026 16:40:03 +0545 Subject: [PATCH 1/3] Add Kadane's algorithm for maximum subarray sum --- dynamic_programming/kadanes_algorithm.py | 53 ++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 dynamic_programming/kadanes_algorithm.py diff --git a/dynamic_programming/kadanes_algorithm.py b/dynamic_programming/kadanes_algorithm.py new file mode 100644 index 000000000000..2360e1029da1 --- /dev/null +++ b/dynamic_programming/kadanes_algorithm.py @@ -0,0 +1,53 @@ +""" +Kadane's Algorithm: Given an array of integers, find the contiguous subarray +with the largest sum. + +Example: + [-2, 1, -3, 4, -1, 2, 1, -5, 4] --> 6 (subarray [4, -1, 2, 1]) +""" + + +def kadanes_algorithm(arr: list[int]) -> int: + """ + Finds the maximum sum of a contiguous subarray using Kadane's Algorithm. + + Parameters + ---------- + arr: list[int], the input list of integers + + Returns + ------- + int: the maximum subarray sum + + >>> kadanes_algorithm([-2, 1, -3, 4, -1, 2, 1, -5, 4]) + 6 + >>> kadanes_algorithm([1]) + 1 + >>> kadanes_algorithm([-1, -2, -3]) + -1 + >>> kadanes_algorithm([5, 4, -1, 7, 8]) + 23 + >>> kadanes_algorithm([0, 0, 0]) + 0 + >>> kadanes_algorithm([-2, -3, 4, -1, -2, 1, 5, -3]) + 7 + """ + if not arr: + raise ValueError("Array cannot be empty") + + max_sum = current_sum = arr[0] + + for num in arr[1:]: + current_sum = max(num, current_sum + num) + max_sum = max(max_sum, current_sum) + + return max_sum + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + + arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4] + print(f"Maximum subarray sum: {kadanes_algorithm(arr)}") From 65730becbd7ce1fd7cab1c01c2d9cd61fa17dbde Mon Sep 17 00:00:00 2001 From: Saurav Date: Thu, 9 Apr 2026 16:46:34 +0545 Subject: [PATCH 2/3] Add Wikipedia reference to Kadane's algorithm --- dynamic_programming/kadanes_algorithm.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dynamic_programming/kadanes_algorithm.py b/dynamic_programming/kadanes_algorithm.py index 2360e1029da1..6c7cac9e983f 100644 --- a/dynamic_programming/kadanes_algorithm.py +++ b/dynamic_programming/kadanes_algorithm.py @@ -4,8 +4,9 @@ Example: [-2, 1, -3, 4, -1, 2, 1, -5, 4] --> 6 (subarray [4, -1, 2, 1]) -""" +Reference: https://en.wikipedia.org/wiki/Maximum_subarray_problem +""" def kadanes_algorithm(arr: list[int]) -> int: """ From 21fad4e97adadbe4a33f6dbeacc1be1ea315f424 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 9 Apr 2026 11:03:03 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/kadanes_algorithm.py | 1 + 1 file changed, 1 insertion(+) diff --git a/dynamic_programming/kadanes_algorithm.py b/dynamic_programming/kadanes_algorithm.py index 6c7cac9e983f..5b38ec621715 100644 --- a/dynamic_programming/kadanes_algorithm.py +++ b/dynamic_programming/kadanes_algorithm.py @@ -8,6 +8,7 @@ Reference: https://en.wikipedia.org/wiki/Maximum_subarray_problem """ + def kadanes_algorithm(arr: list[int]) -> int: """ Finds the maximum sum of a contiguous subarray using Kadane's Algorithm.