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

Added Kadane's algorithm #2050 #2065

Closed
wants to merge 3 commits into from
Closed
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
70 changes: 70 additions & 0 deletions C++/KadaneAlgorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Date:- 2/10/2023
// Kadane Algorithm in C++

#include <iostream>
#include <vector>
#include <climits>

using namespace std;

// Structure to store information about a subarray
struct Subarray {
int start; // Starting index of the subarray
int end; // Ending index of the subarray
int sum; // Sum of the subarray
};

/**
* Kadane's Algorithm for finding the maximum subarray sum.
*
* @param arr The input array.
* @return Subarray structure representing the maximum subarray sum and its indices.
*/
Subarray kadane_algorithm(const vector<int>& arr) {
Subarray current;
current.sum = INT_MIN;
Subarray max;
max.sum = INT_MIN;

for (int i = 0; i < arr.size(); i++) {
// If current sum is negative, start a new subarray
if (current.sum < 0) {
current.start = i;
current.end = i;
current.sum = arr[i];
} else {
// Extend the current subarray
current.end = i;
current.sum += arr[i];
}

// Update max if current sum is greater
if (current.sum > max.sum) {
max = current;
}
}

return max;
}

int main() {
int num_elements;
cout << "Enter the number of elements: ";
cin >> num_elements;

vector<int> input_list;
cout << "Enter the elements, one by one:" << endl;
for (int i = 0; i < num_elements; i++) {
int element;
cin >> element;
input_list.push_back(element);
}

Subarray max_subarray = kadane_algorithm(input_list);

cout << "Maximum Subarray Sum: " << max_subarray.sum << endl;
cout << "Subarray Start Index: " << max_subarray.start << endl;
cout << "Subarray End Index: " << max_subarray.end << endl;

return 0;
}