mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-05 11:40:46 +08:00
feat: update to CXX standard 17 and add CMakeLists file to directories without them (#2746)
* chore: add cache and build comment to git ignore
* fix: add cmakelists to dynamic programming
* fix: add cmakelists to greedy_algorithms
* fix: add cmakelists to operations_on_datastructures
* fix: add cmakelists to range_queries
* fix: add `dynamic_programmin`, `greedy_algorithms`, `range_queries` and `operations_on_datastructures` subdirectories to cmakelists.txt
* fix: init of transform_reduce in dynamic_programming
* fix: add an include for functional in catalan_numbers
* chore: bump CXX standard to 20
* revert: bump CXX standard to 20
* chore: bump c++ version to 17 and add justification
Arm supports c++ 17
Esp32 supports c++ 23
decision was made to be 17 because it seemed to offer the best combatability
* fix: compilation error in catalan numbers
* fix: add <set> header to longest increasing subsequence nlogn
* fix: add cmath & algorithm header to mo.cpp
* fix: remove register key word from fast integer
* fix: replace using namespace std with std::cin and std::cout
* docs: typo in c++17
* fix: memory leak in bellman_ford
* fix: typo in bellman_ford
* fix: typo in word_break
* fix: dynamic array in coin_change
* fix dynamic array in egg_dropping puzzle
* chore: remove unnecessary comment
* fix: add vla to be an error
* chore: add extra warnings
* fix: use add_compile options instead of set()
* fix: compile options are not strings
* fix: vla in floyd_warshall
* fix: vla in egg_dropping_puzzel
* fix: vla in coin_change
* fix: vla in edit_distance
* fix: vla in floyd_warshall
* feat: remove kadane and replace it with kadane2
* fix: vla in longest_common_subsequence
* fix: int overflow in floyd_warshall
* fix: vla in lisnlogn
* fix: use const vector& instead of array
* fix: use dynamic array instead of vla in knapsack
* fix: use of and in msvc is unsupported by default adding permissive flag fixes it
* test: make executables the tests themselves
* Revert "test: make executables the tests themselves"
This reverts commit 7a16c31c4e.
* fix: make dist constant in print
* fix: namespace issue in unbounded_0_1
* fix: include cstdint to fix compilation
This commit is contained in:
@@ -1,29 +1,74 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Implementation of [Kadane
|
||||
* Algorithm](https://en.wikipedia.org/wiki/Kadane%27s_algorithm)
|
||||
*
|
||||
* @details
|
||||
* 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
|
||||
*
|
||||
* ### Algorithm
|
||||
* 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
|
||||
*
|
||||
* @author [Ayush Singh](https://github.com/ayush523)
|
||||
*/
|
||||
#include <array>
|
||||
#include <climits>
|
||||
#include <iostream>
|
||||
|
||||
int maxSubArraySum(int a[], int size) {
|
||||
int max_so_far = INT_MIN, max_ending_here = 0;
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
max_ending_here = max_ending_here + a[i];
|
||||
if (max_so_far < max_ending_here)
|
||||
max_so_far = max_ending_here;
|
||||
|
||||
if (max_ending_here < 0)
|
||||
max_ending_here = 0;
|
||||
/**
|
||||
* @namespace dynamic_programming
|
||||
* @brief Dynamic Programming algorithms
|
||||
*/
|
||||
namespace dynamic_programming {
|
||||
/**
|
||||
* @namespace kadane
|
||||
* @brief Functions for
|
||||
* [Kadane](https://en.wikipedia.org/wiki/Kadane%27s_algorithm) algorithm.
|
||||
*/
|
||||
namespace kadane {
|
||||
/**
|
||||
* @brief 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
|
||||
* @tparam N number of array size
|
||||
* @param n array where numbers are saved
|
||||
* @returns the value of maximum subarray sum
|
||||
*/
|
||||
template <size_t N>
|
||||
int maxSubArray(const std::array<int, N> &n) {
|
||||
int curr_sum =
|
||||
0; // declaring a variable named as curr_sum and initialized it to 0
|
||||
int max_sum = INT_MIN; // Initialized max_sum to INT_MIN
|
||||
for (int i : n) { // for loop to iterate over the elements of the array
|
||||
curr_sum += n[i];
|
||||
max_sum = std::max(max_sum, curr_sum); // getting the maximum value
|
||||
curr_sum = std::max(curr_sum, 0); // updating the value of curr_sum
|
||||
}
|
||||
return max_so_far;
|
||||
return max_sum; // returning the value of max_sum
|
||||
}
|
||||
} // namespace kadane
|
||||
} // namespace dynamic_programming
|
||||
|
||||
/**
|
||||
* @brief Main function
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main() {
|
||||
int n, i;
|
||||
std::cout << "Enter the number of elements \n";
|
||||
std::cin >> n;
|
||||
int a[n]; // NOLINT
|
||||
for (i = 0; i < n; i++) {
|
||||
std::cin >> a[i];
|
||||
const int N = 5;
|
||||
std::array<int, N> n{}; // declaring array
|
||||
// taking values of elements from user
|
||||
for (int i = 0; i < n.size(); i++) {
|
||||
std::cout << "Enter value of n[" << i << "]"
|
||||
<< "\n";
|
||||
std::cin >> n[i];
|
||||
}
|
||||
int max_sum = maxSubArraySum(a, n);
|
||||
std::cout << "Maximum contiguous sum is " << max_sum;
|
||||
int max_sum = dynamic_programming::kadane::maxSubArray<N>(
|
||||
n); // calling maxSubArray function
|
||||
std::cout << "Maximum subarray sum is " << max_sum; // Printing the answer
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user