-
-
Notifications
You must be signed in to change notification settings - Fork 156
Create Kadanes.md #1029
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
Merged
Merged
Create Kadanes.md #1029
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
--- | ||
id: Algorithms | ||
title: Kadanes Algorithm | ||
sidebar_label: Algorithms | ||
ajay-dhangar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sidebar_position: 2 | ||
description: "Kadane's Algorithm efficiently finds the maximum sum contiguous subarray in a one-dimensional array. It's a dynamic programming technique named after its creator, Jay Kadane." | ||
--- | ||
## Kadane's Algorithm: | ||
|
||
- **Purpose**: Finds the maximum sum of a contiguous subarray. | ||
- **Time Complexity**: O(n) | ||
ajay-dhangar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- **Space Complexity**: O(1) | ||
- **Use Case**: Optimal for solving maximum subarray sum problems in linear time. | ||
### Simple Explanation | ||
Kadane's Algorithm: | ||
|
||
1. **Initialize**: | ||
- Set `maxSum` to a very small number (e.g., negative infinity). | ||
- Set `currentSum` to 0. | ||
|
||
2. **Iterate Through the Array**: | ||
- For each element in the array: | ||
- Add the element to `currentSum`. | ||
- Update `maxSum` if `currentSum` is greater than `maxSum`. | ||
- If `currentSum` becomes negative, reset it to 0. | ||
|
||
3. **Result**: | ||
- The value of `maxSum` at the end of the iteration is the maximum sum of any contiguous subarray. | ||
### Code | ||
|
||
Input: arr = {-2,-3,4,-1,-2,1,5,-3} | ||
|
||
Output: 7 | ||
|
||
Explanation: The subarray {4,-1, -2, 1, 5} has the largest sum 7. | ||
|
||
### Python | ||
ajay-dhangar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def max_sub_array(nums): | ||
max_sum = float('-inf') | ||
current_sum = 0 | ||
|
||
for num in nums: | ||
# Add the current number to the current_sum | ||
current_sum += num | ||
# Update max_sum if current_sum is greater | ||
max_sum = max(max_sum, current_sum) | ||
# Reset current_sum to 0 if it drops below 0 | ||
if current_sum < 0: | ||
current_sum = 0 | ||
|
||
return max_sum | ||
|
||
### C++ | ||
// Kadane's Algorithm to find the maximum sum of a contiguous subarray | ||
ajay-dhangar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <algorithm> | ||
#include <climits> | ||
|
||
int maxSubArray(const std::vector<int>& nums) { | ||
// Initialize max_sum to the lowest possible value and current_sum to 0 | ||
int max_sum = INT_MIN; | ||
int current_sum = 0; | ||
|
||
for (int num : nums) { | ||
// Add the current number to the current_sum | ||
current_sum += num; | ||
// Update max_sum if current_sum is greater | ||
max_sum = std::max(max_sum, current_sum); | ||
// Reset current_sum to 0 if it drops below 0 | ||
if (current_sum < 0) { | ||
current_sum = 0; | ||
} | ||
} | ||
|
||
return max_sum; | ||
} | ||
|
||
### Explanation | ||
Explanation: | ||
|
||
#### Initialization: | ||
|
||
-`max_sum` is initialized to `INT_MIN` to handle arrays with all negative numbers. | ||
|
||
-`current_sum` starts at `0`. | ||
|
||
#### Iteration: | ||
|
||
-Traverse through each number in the array: | ||
|
||
-Add the number to `current_sum`. | ||
|
||
-Update `max_sum` to be the maximum of `max_sum` and `current_sum`. | ||
|
||
-If `current_sum` becomes negative, reset it to `0` to start a new subarray. | ||
|
||
|
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.