From c476fdaef4070eb17d9604502998174a09db0d17 Mon Sep 17 00:00:00 2001 From: Kam149 <37494508+Kam149@users.noreply.github.com> Date: Sat, 31 Oct 2020 18:31:27 +0530 Subject: [PATCH] Create Kandanes Algorithm.cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Kadane’s algorithm is used to find out the maximum subarray sum from an array of integers. --- .../Kandanes Algorithm.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 common_ds_algo_problems/Kandanes Algorithm.cpp diff --git a/common_ds_algo_problems/Kandanes Algorithm.cpp b/common_ds_algo_problems/Kandanes Algorithm.cpp new file mode 100644 index 0000000..fcd532f --- /dev/null +++ b/common_ds_algo_problems/Kandanes Algorithm.cpp @@ -0,0 +1,30 @@ +#include +#include +using namespace std; + +#define MAX(X, Y) (X > Y) ? X : Y +#define POS(X) (X > 0) ? X : 0 + +int maxSum = INT_MIN; +int num; +int kadane(int* row, int len) +{ + int x, sum, maxSum = INT_MIN; + for (sum = POS(row[0]), x = 0; x < num; ++x, sum = POS(sum + row[x])) + maxSum = MAX(sum, maxSum); + return maxSum; +} + +int main() +{ + cout << "Enter the array length: "; + cin >> n; + int arr[n]; + cout << "Enter the array: "; + for (int i = 0; i < n; i++) + { + cin >> arr[i]; + } + cout << "The Max Sum is: "<