Open
Conversation
Author
|
dont forget to add |
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.
Description:
This pull request adds the implementation of Kadane's Algorithm to find the maximum sum of a contiguous subarray in the given array. This change enhances the repository by providing a solution for a common and efficient algorithm used in competitive programming and placement preparation. The implementation follows standard coding guidelines and includes comments for clarity.
Type of change:
New feature (non-breaking change which adds functionality)
Checklist:
My code follows the style guidelines of this project.
I have performed a self-review of my own code.
I have commented my code, particularly in hard-to-understand areas.
I have made corresponding changes to the documentation.
My changes generate no new warnings.
I have added tests that prove my fix is effective or that my feature works.
New and existing unit tests pass locally with my changes.
Any dependent changes have been merged and published in downstream modules.
Necessary documentation changes done
I have added the file in the correct directory.
Kadane's Algorithm Implementation:
code
class Solution {
public:
// Function to find the sum of contiguous subarray with maximum sum.
int maxSubarraySum(vector &arr) {
// code here...
int maxi = INT_MIN, prefix = 0;
int n = arr.size();
for(int i = 0; i < n; i++) {
prefix += arr[i];
maxi = max(prefix, maxi);
if(prefix < 0)
prefix = 0;
}
return maxi;
}
};