diff --git a/DIRECTORY.md b/DIRECTORY.md index 6c6e7711e..c6ea201b5 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -248,6 +248,7 @@ * [Fast Integer Input](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/fast_integer_input.cpp) * [Happy Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/happy_number.cpp) * [Iterative Tree Traversals](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/iterative_tree_traversals.cpp) + * [Kadanes3](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/kadanes3.cpp) * [Lru Cache](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/lru_cache.cpp) * [Matrix Exponentiation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/matrix_exponentiation.cpp) * [Palindrome Of Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/palindrome_of_number.cpp) diff --git a/others/kadanes3.cpp b/others/kadanes3.cpp new file mode 100644 index 000000000..9cc6604e9 --- /dev/null +++ b/others/kadanes3.cpp @@ -0,0 +1,86 @@ +/** + * @file + * @brief Efficient implementation for maximum contiguous subarray sum by + * [Kadane's + * algorithm](https://www.geeksforgeeks.org/largest-sum-contiguous-subarray/). + * @details + * Our task is to take length of array and then the whole array as input from + * the user and then calculate the maximum contiguos subarray sum for the + * input array, using the kadane's algorithm. + * + * There can be a case that all the elements in the input array are negative. + * In that case, the least value among all elements is the maximum sum with + * subarray length = 1. + * @author [Abhijeet Tiwari](https://github.com/thisabhijeet) + */ + +#include /// for std::array +#include /// for assert +#include /// for INT_MIN value +#include /// for IO operations + +/** + * @brief Utility function to check the current maximum number + * \param arr input array + * \param length length of the input array + * \returns maximum contiguous subarray sum + */ +template +int max_subarray_sum(std::array arr, uint64_t length) { + int64_t current_max = INT_MIN, current_sum = 0; + for (int i = 0; i < length; i++) { + current_sum = current_sum + arr[i]; + if (current_max < current_sum) { + current_max = current_sum; + } + + if (current_sum < 0) { + current_sum = 0; + } + } + return current_max; +} + +/** + * @brief Self-test implementations + * @returns void + */ +static void test() { + std::array arr = {1, 2, 3, 4}; + std::array arr1 = {-1, -2, -4, -6, 7}; + assert(max_subarray_sum(arr, 4) == 10); + assert(max_subarray_sum(arr1, 5) == 7); + std::cout << "All test cases have passed!\n"; +} + +/** + * @brief Main function + * @returns 0 on exit + */ +int main() { + // Below is the code for accepting array from the user and then + // calling the function for the required output. + // It has been commented for now so that the test() function can run + // and the test cases can be verified. + // code for accepting array from user starts + + // std::size_t n; // variable for length of input array + // std::cout << "Enter length of the array: "; + // std::cin >> n; + // std::array arr = {0}; + // // we need to give a constant in size. Hence we have allocated 100 + // for now. + // for (int i = 0; i < n; i++) + // taking input of each element of the array + // { + // std::cin >> arr[i]; + // } + // code for accepting array from user ends + + // int max_sum = max_subarray_sum(arr, n); + // std::cout << "Maximum contiguous sum for this array is : " << max_sum + // << std::endl; + + test(); // run self-test implementations + return 0; +}