Skip to content
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

Kadane's Algorithm #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions Algorithms/Cpp/kadanesAlgorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <iostream>
using namespace std;

// leetcode 53. Maximum Subarray
// this question will provide you a clear idea of kadane's Algo.

// Given an integer array nums, find the subarray with the largest sum, and return its sum.

// Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
// Output: 6
// Explanation: The subarray [4,-1,2,1] has the largest sum 6.

// Input: nums = [1]
// Output: 1
// Explanation: The subarray [1] has the largest sum 1.

// Input: nums = [5,4,-1,7,8]
// Output: 23
// Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.

class Solution {
public:
// Method to find the maximum sum of a subarray within the given vector
int maxSubArray(vector<int>& nums) {
int n = nums.size();
int maxi = INT_MIN, Sum = 0;

// Iterate through the elements of the vector
for (int i = 0; i < n; i++) {
Sum += nums[i]; // Accumulate the sum of the current subarray

// Update the maximum sum seen so far
maxi = max(maxi, Sum);

// If the accumulated sum becomes negative, reset it to 0,
// as it won't be useful in finding the maximum sum subarray.
Sum = max(Sum, 0);
}

// Return the maximum sum of subarray found
return maxi;
}
};

int main() {
// Initialize the vector nums with fixed values
vector<int> nums;
nums.push_back(-2);
nums.push_back(1);
nums.push_back(-3);
nums.push_back(4);
nums.push_back(-1);
nums.push_back(2);
nums.push_back(1);
nums.push_back(-5);
nums.push_back(4);

// Create an instance of the Solution class
Solution solution;

// Call the maxSubArray method to find the maximum sum of the subarray
int maxSum = solution.maxSubArray(nums);

// Print the result to the console
cout << "Maximum sum of subarray: " << maxSum << endl;

return 0;
}