feat-kadane's algo#222
Merged
siriak merged 7 commits intoTheAlgorithms:masterfrom Oct 23, 2025
Merged
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR implements Kadane's algorithm for finding the maximum-sum contiguous subarray in O(n) time and O(1) space. The implementation includes comprehensive edge-case handling and a circular variant.
Key Changes:
- Core
kadane()function with proper handling of empty, single-element, and all-negative arrays - Circular array variant
kadane_circular()for wrap-around maximum subarray detection - Comprehensive test suite covering edge cases and performance validation
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Purpose: Implements Kadane’s algorithm to find the maximum-sum contiguous subarray in a numeric vector.
Time / Space: O(n) time, O(1) extra space (excluding output subarray).
Main function: kadane(arr) — returns a list: max_sum, start (1-based), end (1-based), and subarray.
Edge-case handling: correct behavior for empty arrays (returns -Inf and NA indices) and all-negative arrays (returns the largest element).
Initialization: starts with the first element so single-element and negative-only inputs are handled naturally.
Tie behavior: returns the first-occurring maximum subarray (current code favors earlier segments).
Helper utilities: print_kadane_result() for nicely formatted output of results.
Tests included: mixed signs, all-positive, all-negative, single-element, empty array, and large random array (with timing).
Variant provided: kadane_circular() — computes max subarray sum for circular/wrap-around arrays (uses total sum − min-subarray trick); indices for wrap-around are not computed in that variant by default.
Extensibility: easy to adapt to return all maximum subarrays, or to compute wrap-around start/end indices, or to track multiple occurrences.