TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Implementation of Kadane Algorithm More...
#include <array>
#include <climits>
#include <iostream>
Go to the source code of this file.
Namespaces | |
namespace | dynamic_programming |
Dynamic Programming algorithms. | |
namespace | kadane |
Functions for Kadane algorithm. | |
Functions | |
template<size_t N> | |
int | dynamic_programming::kadane::maxSubArray (const std::array< int, N > &n) |
maxSubArray function is used to calculate the maximum sum subarray and returns the value of maximum sum which is stored in the variable max_sum | |
int | main () |
Main function. | |
Implementation of Kadane Algorithm
Kadane algorithm is used to find the maximum sum subarray in an array and maximum sum subarray problem is the task of finding a contiguous subarray with the largest sum
The simple idea of the algorithm is to search for all positive contiguous segments of the array and keep track of maximum sum contiguous segment among all positive segments(curr_sum is used for this) Each time we get a positive sum we compare it with max_sum and update max_sum if it is greater than curr_sum
Definition in file kadane.cpp.
int main | ( | void | ) |
Main function.
Definition at line 60 of file kadane.cpp.
int dynamic_programming::kadane::maxSubArray | ( | const std::array< int, N > & | n | ) |
maxSubArray function is used to calculate the maximum sum subarray and returns the value of maximum sum which is stored in the variable max_sum
N | number of array size |
n | array where numbers are saved |
Definition at line 42 of file kadane.cpp.