Create Kadane's Algorithm #1362
Closed
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.
PR Description
Theory
Kadane's Algorithm is used to find the maximum sum of a contiguous subarray within a one-dimensional numerical array of integers. It efficiently handles both positive and negative numbers and is known for its linear time complexity.
Algorithm
Initialization:
Iterate through the array:
For each element in the array:
Update max_current to be the maximum of the current element itself or the sum of max_current and the current element (this decides whether to start a new subarray or to continue the existing one).
Update max_global to be the maximum of max_global and max_current.
Result:
Time Complexity
O(n): The algorithm runs in linear time because it makes a single pass through the array.
Space Complexity
O(1): The algorithm uses only a constant amount of extra space.
Related Issues
-Fixes #1361
Checklist
mainbranch before making this PRUndertaking
I declare that:
I understand that any violation of this undertaking may have legal consequences that I will bear and could result in the withdrawal of any recognition associated with the work.