|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief Implementation of [Kadane |
| 4 | + * Algorithm](https://en.wikipedia.org/wiki/Kadane%27s_algorithm) |
| 5 | + * |
| 6 | + * @details |
| 7 | + * Kadane algorithm is used to find the maximum sum subarray in an array and |
| 8 | + * maximum sum subarray problem is the task of finding a contiguous subarray |
| 9 | + * with the largest sum |
| 10 | + * |
| 11 | + * ### Algorithm |
| 12 | + * The simple idea of the algorithm is to search for all positive |
| 13 | + * contiguous segments of the array and keep track of maximum sum contiguous |
| 14 | + * segment among all positive segments(curr_sum is used for this) |
| 15 | + * Each time we get a positive sum we compare it with max_sum and update max_sum |
| 16 | + * if it is greater than curr_sum |
| 17 | + * |
| 18 | + * @author [Ayush Singh](https://github.com/ayush523) |
| 19 | + */ |
| 20 | +#include <array> |
1 | 21 | #include <climits>
|
2 | 22 | #include <iostream>
|
3 |
| - |
4 |
| -int maxSubArraySum(int a[], int size) { |
5 |
| - int max_so_far = INT_MIN, max_ending_here = 0; |
6 |
| - |
7 |
| - for (int i = 0; i < size; i++) { |
8 |
| - max_ending_here = max_ending_here + a[i]; |
9 |
| - if (max_so_far < max_ending_here) |
10 |
| - max_so_far = max_ending_here; |
11 |
| - |
12 |
| - if (max_ending_here < 0) |
13 |
| - max_ending_here = 0; |
| 23 | +/** |
| 24 | + * @namespace dynamic_programming |
| 25 | + * @brief Dynamic Programming algorithms |
| 26 | + */ |
| 27 | +namespace dynamic_programming { |
| 28 | +/** |
| 29 | + * @namespace kadane |
| 30 | + * @brief Functions for |
| 31 | + * [Kadane](https://en.wikipedia.org/wiki/Kadane%27s_algorithm) algorithm. |
| 32 | + */ |
| 33 | +namespace kadane { |
| 34 | +/** |
| 35 | + * @brief maxSubArray function is used to calculate the maximum sum subarray |
| 36 | + * and returns the value of maximum sum which is stored in the variable max_sum |
| 37 | + * @tparam N number of array size |
| 38 | + * @param n array where numbers are saved |
| 39 | + * @returns the value of maximum subarray sum |
| 40 | + */ |
| 41 | +template <size_t N> |
| 42 | +int maxSubArray(const std::array<int, N> &n) { |
| 43 | + int curr_sum = |
| 44 | + 0; // declaring a variable named as curr_sum and initialized it to 0 |
| 45 | + int max_sum = INT_MIN; // Initialized max_sum to INT_MIN |
| 46 | + for (int i : n) { // for loop to iterate over the elements of the array |
| 47 | + curr_sum += n[i]; |
| 48 | + max_sum = std::max(max_sum, curr_sum); // getting the maximum value |
| 49 | + curr_sum = std::max(curr_sum, 0); // updating the value of curr_sum |
14 | 50 | }
|
15 |
| - return max_so_far; |
| 51 | + return max_sum; // returning the value of max_sum |
16 | 52 | }
|
| 53 | +} // namespace kadane |
| 54 | +} // namespace dynamic_programming |
17 | 55 |
|
| 56 | +/** |
| 57 | + * @brief Main function |
| 58 | + * @returns 0 on exit |
| 59 | + */ |
18 | 60 | int main() {
|
19 |
| - int n, i; |
20 |
| - std::cout << "Enter the number of elements \n"; |
21 |
| - std::cin >> n; |
22 |
| - int a[n]; // NOLINT |
23 |
| - for (i = 0; i < n; i++) { |
24 |
| - std::cin >> a[i]; |
| 61 | + const int N = 5; |
| 62 | + std::array<int, N> n{}; // declaring array |
| 63 | + // taking values of elements from user |
| 64 | + for (int i = 0; i < n.size(); i++) { |
| 65 | + std::cout << "Enter value of n[" << i << "]" |
| 66 | + << "\n"; |
| 67 | + std::cin >> n[i]; |
25 | 68 | }
|
26 |
| - int max_sum = maxSubArraySum(a, n); |
27 |
| - std::cout << "Maximum contiguous sum is " << max_sum; |
| 69 | + int max_sum = dynamic_programming::kadane::maxSubArray<N>( |
| 70 | + n); // calling maxSubArray function |
| 71 | + std::cout << "Maximum subarray sum is " << max_sum; // Printing the answer |
| 72 | + |
28 | 73 | return 0;
|
29 | 74 | }
|
0 commit comments