Kavya-24 / Snippets

Hacktoberfest 2024
41 stars 278 forks source link

Create Kadane_algorithm.cpp #445

Open dev-priyanshu15 opened 1 month ago

dev-priyanshu15 commented 1 month ago

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

dev-priyanshu15 commented 1 month ago

dont forget to add hacktoberfest-accepted label please