From 8129c0e736a98135ee62267ac075c913a8fd42c5 Mon Sep 17 00:00:00 2001 From: Tanish22 Date: Tue, 7 Oct 2025 22:40:37 +0530 Subject: [PATCH 1/6] Add Kadane's Algorithm to dp/ folder Added Kadane's Algorithm to the dp/ folder to find the maximum sum of a contiguous subarray. The implementation follows TheAlgorithms/Python guidelines: - Returns both maximum sum and the corresponding subarray. - Includes type hints, docstring, and doctests. - References Wikipedia for source: https://en.wikipedia.org/wiki/Maximum_subarray_problem Example: >>> kadane([-2,1,-3,4,-1,2,1,-5,4]) (6, [4, -1, 2, 1]) --- dynamic_programming/kadane_algorithm.py | 62 +++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 dynamic_programming/kadane_algorithm.py diff --git a/dynamic_programming/kadane_algorithm.py b/dynamic_programming/kadane_algorithm.py new file mode 100644 index 000000000000..8b7ef8254b9c --- /dev/null +++ b/dynamic_programming/kadane_algorithm.py @@ -0,0 +1,62 @@ +""" +Kadane's Algorithm implementation in Python. + +Finds the maximum sum of a contiguous subarray within a one-dimensional array of numbers. + +Source: +https://en.wikipedia.org/wiki/Maximum_subarray_problem +""" + +from typing import List, Tuple + + +def kadane(arr: List[int]) -> Tuple[int, List[int]]: + """ + Returns the maximum sum of a contiguous subarray and the subarray itself. + + Parameters + ---------- + arr : List[int] + List of integers (can be positive, negative, or zero). + + Returns + ------- + Tuple[int, List[int]] + Maximum subarray sum and the corresponding subarray. + + Examples + -------- + >>> kadane([-2,1,-3,4,-1,2,1,-5,4]) + (6, [4, -1, 2, 1]) + + >>> kadane([1,2,3,4]) + (10, [1, 2, 3, 4]) + + >>> kadane([-1,-2,-3]) + (-1, [-1]) + """ + if not arr: + raise ValueError("Input array cannot be empty") + + max_current = max_global = arr[0] + start = end = s = 0 + + for i in range(1, len(arr)): + if arr[i] > max_current + arr[i]: + max_current = arr[i] + s = i + else: + max_current += arr[i] + + if max_current > max_global: + max_global = max_current + start = s + end = i + + return max_global, arr[start:end+1] + + +# Doctest runner +if __name__ == "__main__": + import doctest + doctest.testmod() From ec206e1bb0f92fdb78baeb3878f40010f3ec5dc5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 7 Oct 2025 17:19:36 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/kadane_algorithm.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/dynamic_programming/kadane_algorithm.py b/dynamic_programming/kadane_algorithm.py index 8b7ef8254b9c..468e9962f2f0 100644 --- a/dynamic_programming/kadane_algorithm.py +++ b/dynamic_programming/kadane_algorithm.py @@ -28,10 +28,10 @@ def kadane(arr: List[int]) -> Tuple[int, List[int]]: -------- >>> kadane([-2,1,-3,4,-1,2,1,-5,4]) (6, [4, -1, 2, 1]) - + >>> kadane([1,2,3,4]) (10, [1, 2, 3, 4]) - + >>> kadane([-1,-2,-3]) (-1, [-1]) """ @@ -53,10 +53,11 @@ def kadane(arr: List[int]) -> Tuple[int, List[int]]: start = s end = i - return max_global, arr[start:end+1] + return max_global, arr[start : end + 1] # Doctest runner if __name__ == "__main__": import doctest + doctest.testmod() From 944c5f9a83850f735b8d6175ab1d6cdd03bc579e Mon Sep 17 00:00:00 2001 From: Tanish22 Date: Tue, 7 Oct 2025 22:55:16 +0530 Subject: [PATCH 3/6] Update kadane_algorithm.py Removing the usage if typing package and using inbuilt list instead. --- dynamic_programming/kadane_algorithm.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/dynamic_programming/kadane_algorithm.py b/dynamic_programming/kadane_algorithm.py index 468e9962f2f0..5ff3ec49b7cc 100644 --- a/dynamic_programming/kadane_algorithm.py +++ b/dynamic_programming/kadane_algorithm.py @@ -7,31 +7,29 @@ https://en.wikipedia.org/wiki/Maximum_subarray_problem """ -from typing import List, Tuple - -def kadane(arr: List[int]) -> Tuple[int, List[int]]: +def kadane(arr): """ Returns the maximum sum of a contiguous subarray and the subarray itself. Parameters ---------- - arr : List[int] + arr : list List of integers (can be positive, negative, or zero). Returns ------- - Tuple[int, List[int]] + tuple Maximum subarray sum and the corresponding subarray. Examples -------- >>> kadane([-2,1,-3,4,-1,2,1,-5,4]) (6, [4, -1, 2, 1]) - + >>> kadane([1,2,3,4]) (10, [1, 2, 3, 4]) - + >>> kadane([-1,-2,-3]) (-1, [-1]) """ @@ -53,11 +51,10 @@ def kadane(arr: List[int]) -> Tuple[int, List[int]]: start = s end = i - return max_global, arr[start : end + 1] + return max_global, arr[start:end+1] # Doctest runner if __name__ == "__main__": import doctest - doctest.testmod() From 02fa4330945a5bf74b212700e313edd3d875b868 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 7 Oct 2025 17:25:43 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/kadane_algorithm.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/dynamic_programming/kadane_algorithm.py b/dynamic_programming/kadane_algorithm.py index 5ff3ec49b7cc..88854257b7dd 100644 --- a/dynamic_programming/kadane_algorithm.py +++ b/dynamic_programming/kadane_algorithm.py @@ -26,10 +26,10 @@ def kadane(arr): -------- >>> kadane([-2,1,-3,4,-1,2,1,-5,4]) (6, [4, -1, 2, 1]) - + >>> kadane([1,2,3,4]) (10, [1, 2, 3, 4]) - + >>> kadane([-1,-2,-3]) (-1, [-1]) """ @@ -51,10 +51,11 @@ def kadane(arr): start = s end = i - return max_global, arr[start:end+1] + return max_global, arr[start : end + 1] # Doctest runner if __name__ == "__main__": import doctest + doctest.testmod() From 87010e19a7a44b272bde278961862984a18456a6 Mon Sep 17 00:00:00 2001 From: Tanish22 Date: Wed, 8 Oct 2025 18:54:50 +0530 Subject: [PATCH 5/6] Update kadane_algorithm.py resolved all the issue occured during merging --- dynamic_programming/kadane_algorithm.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dynamic_programming/kadane_algorithm.py b/dynamic_programming/kadane_algorithm.py index 88854257b7dd..39d59dd922e9 100644 --- a/dynamic_programming/kadane_algorithm.py +++ b/dynamic_programming/kadane_algorithm.py @@ -1,14 +1,15 @@ """ Kadane's Algorithm implementation in Python. -Finds the maximum sum of a contiguous subarray within a one-dimensional array of numbers. +Finds the maximum sum of a contiguous subarray within +a one-dimensional array of numbers. Source: https://en.wikipedia.org/wiki/Maximum_subarray_problem """ -def kadane(arr): +def kadane(arr: list[int]) -> tuple[int, list[int]]: """ Returns the maximum sum of a contiguous subarray and the subarray itself. @@ -51,11 +52,10 @@ def kadane(arr): start = s end = i - return max_global, arr[start : end + 1] + return max_global, arr[start:end+1] # Doctest runner if __name__ == "__main__": import doctest - doctest.testmod() From c017fa37af5f4ce48718d1613849f9289a8eaeea Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 8 Oct 2025 13:25:11 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dynamic_programming/kadane_algorithm.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dynamic_programming/kadane_algorithm.py b/dynamic_programming/kadane_algorithm.py index 39d59dd922e9..757e316c1da8 100644 --- a/dynamic_programming/kadane_algorithm.py +++ b/dynamic_programming/kadane_algorithm.py @@ -52,10 +52,11 @@ def kadane(arr: list[int]) -> tuple[int, list[int]]: start = s end = i - return max_global, arr[start:end+1] + return max_global, arr[start : end + 1] # Doctest runner if __name__ == "__main__": import doctest + doctest.testmod()