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;
}
};
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;
}
};