Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0d766b0

Browse files
authoredNov 4, 2024
feat: update to CXX standard 17 and add CMakeLists file to directories without them (TheAlgorithms#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 7a16c31. * fix: make dist constant in print * fix: namespace issue in unbounded_0_1 * fix: include cstdint to fix compilation
1 parent c6af943 commit 0d766b0

35 files changed

+377
-268
lines changed
 

‎.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,9 @@ a.out
3434
*.out
3535
*.app
3636

37+
# Cache
38+
.cache/
39+
40+
# Build
3741
build/
3842
git_diff.txt

‎CMakeLists.txt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ project(Algorithms_in_C++
55
DESCRIPTION "Set of algorithms implemented in C++."
66
)
77

8-
# set(CMAKE_CXX_CPPLINT "~/anaconda3/bin/cpplint --filter=-legal/copyright --std=c++11")
9-
# find_program(CLANG_FORMAT "clang-format")
10-
11-
set(CMAKE_CXX_STANDARD 11)
8+
# C++ standard
9+
set(CMAKE_CXX_STANDARD 17)
1210
set(CMAKE_CXX_STANDARD_REQUIRED ON)
1311

12+
# Additional warnings and errors
1413
if(MSVC)
15-
# set(CMAKE_CXX_STANDARD 14)
1614
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
17-
endif(MSVC)
15+
add_compile_options(/W4 /permissive-)
16+
else()
17+
add_compile_options(-Wall -Wextra -Wno-register -Werror=vla)
18+
endif()
1819

1920
option(USE_OPENMP "flag to use OpenMP for multithreading" ON)
2021
if(USE_OPENMP)
@@ -38,6 +39,10 @@ add_subdirectory(graphics)
3839
add_subdirectory(probability)
3940
add_subdirectory(backtracking)
4041
add_subdirectory(bit_manipulation)
42+
add_subdirectory(dynamic_programming)
43+
add_subdirectory(greedy_algorithms)
44+
add_subdirectory(range_queries)
45+
add_subdirectory(operations_on_datastructures)
4146
add_subdirectory(data_structures)
4247
add_subdirectory(machine_learning)
4348
add_subdirectory(numerical_methods)

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ This repository is a collection of open-source implementation of a variety of al
2222
* Well documented source code with detailed explanations provide a valuable resource for educators and students alike.
2323
* Each source code is atomic using [STL classes](https://en.wikipedia.org/wiki/Standard_Template_Library) and _no external libraries_ are required for their compilation and execution. Thus, the fundamentals of the algorithms can be studied in much depth.
2424
* Source codes are [compiled and tested](https://github.com/TheAlgorithms/C-Plus-Plus/actions?query=workflow%3A%22Awesome+CI+Workflow%22) for every commit on the latest versions of three major operating systems viz., Windows, MacOS, and Ubuntu (Linux) using MSVC 19 2022, AppleClang 14.0.0, and GNU 11.3.0 respectively.
25-
* Strict adherence to [C++11](https://en.wikipedia.org/wiki/C%2B%2B11) standard ensures portability of code to embedded systems as well like ESP32, ARM Cortex, etc. with little to no changes.
25+
* Strict adherence to [C++17](https://en.wikipedia.org/wiki/C%2B%2B17) standard ensures portability of code to embedded systems as well like [ESP32](https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-guides/cplusplus.html#c-language-standard), [ARM Cortex](https://developer.arm.com/documentation/101458/2404/Standards-support/Supported-C-C---standards-in-Arm-C-C---Compiler), etc. with little to no changes.
2626
* Self-checks within programs ensure correct implementations with confidence.
2727
* Modular implementations and OpenSource licensing enable the functions to be utilized conveniently in other applications.
2828

‎dynamic_programming/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
2+
# with full pathname. RELATIVE may makes it easier to extract an executable name
3+
# automatically.
4+
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp )
5+
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
6+
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
7+
foreach( testsourcefile ${APP_SOURCES} )
8+
# I used a simple string replace, to cut off .cpp.
9+
string( REPLACE ".cpp" "" testname ${testsourcefile} )
10+
add_executable( ${testname} ${testsourcefile} )
11+
12+
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX)
13+
if(OpenMP_CXX_FOUND)
14+
target_link_libraries(${testname} OpenMP::OpenMP_CXX)
15+
endif()
16+
install(TARGETS ${testname} DESTINATION "bin/dynamic_programming")
17+
18+
endforeach( testsourcefile ${APP_SOURCES} )

‎dynamic_programming/abbreviation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525

2626
#include <cassert> /// for `assert`
27+
#include <cstdint> /// for `std::uint32_t`
2728
#include <iostream> /// for IO operations
2829
#include <string> /// for `std::string` library
2930
#include <vector> /// for `std::vector` STL library

‎dynamic_programming/bellman_ford.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#include <limits.h>
1+
#include <climits>
22
#include <iostream>
3+
#include <vector>
34

45
using namespace std;
56

@@ -13,13 +14,13 @@ class Edge {
1314
class Graph {
1415
public:
1516
int vertexNum, edgeNum;
16-
Edge *edges;
17+
std::vector<Edge> edges;
1718

1819
// Constructs a graph with V vertices and E edges
1920
Graph(int V, int E) {
2021
this->vertexNum = V;
2122
this->edgeNum = E;
22-
this->edges = (Edge *)malloc(E * sizeof(Edge));
23+
this->edges.reserve(E);
2324
}
2425

2526
// Adds the given edge to the graph
@@ -36,7 +37,7 @@ class Graph {
3637
};
3738

3839
// Utility function to print distances
39-
void print(int dist[], int V) {
40+
void print(const std::vector<int>& dist, int V) {
4041
cout << "\nVertex Distance" << endl;
4142
for (int i = 0; i < V; i++) {
4243
if (dist[i] != INT_MAX)
@@ -52,7 +53,8 @@ void print(int dist[], int V) {
5253
void BellmanFord(Graph graph, int src) {
5354
int V = graph.vertexNum;
5455
int E = graph.edgeNum;
55-
int dist[V];
56+
std::vector<int> dist;
57+
dist.reserve(E);
5658

5759
// Initialize distances array as INF for all except source
5860
// Intialize source as zero

‎dynamic_programming/catalan_numbers.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
https://oeis.org/A000108/
1010
*/
1111

12-
#include <cassert> /// for assert
13-
#include <cstdint> /// for std::uint64_t
14-
#include <cstdlib> /// for std::size_t
15-
#include <numeric> /// for std::transform_reduce
16-
#include <vector> /// for std::vector
12+
#include <cassert> /// for assert
13+
#include <cstdint> /// for std::uint64_t
14+
#include <cstdlib> /// for std::size_t
15+
#include <functional> /// for std::plus & std::multiplies
16+
#include <numeric> /// for std::transform_reduce
17+
#include <vector> /// for std::vector
1718

1819
/**
1920
* @brief computes and caches Catalan numbers
@@ -24,7 +25,7 @@ class catalan_numbers {
2425

2526
value_type compute_next() {
2627
return std::transform_reduce(known.begin(), known.end(), known.rbegin(),
27-
static_cast<value_type>(), std::plus<>(),
28+
static_cast<value_type>(0), std::plus<>(),
2829
std::multiplies<>());
2930
}
3031

‎dynamic_programming/coin_change.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#include <climits>
22
#include <iostream>
3+
#include <vector>
34
using namespace std;
45

56
// Function to find the Minimum number of coins required to get Sum S
67
int findMinCoins(int arr[], int n, int N) {
78
// dp[i] = no of coins required to get a total of i
8-
int dp[N + 1];
9+
std::vector<int> dp(N + 1);
910

1011
// 0 coins are needed for 0 sum
1112

‎dynamic_programming/cut_rod.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <array>
2222
#include <cassert>
2323
#include <climits>
24+
#include <cstdint>
2425
#include <iostream>
2526
/**
2627
* @namespace dynamic_programming
@@ -70,8 +71,8 @@ int maxProfitByCuttingRod(const std::array<int, T> &price, const uint64_t &n) {
7071
*/
7172
static void test() {
7273
// Test 1
73-
const int16_t n1 = 8; // size of rod
74-
std::array<int32_t, n1> price1 = {1,2,4,6,8,45,21,9}; // price array
74+
const int16_t n1 = 8; // size of rod
75+
std::array<int32_t, n1> price1 = {1, 2, 4, 6, 8, 45, 21, 9}; // price array
7576
const int64_t max_profit1 =
7677
dynamic_programming::cut_rod::maxProfitByCuttingRod(price1, n1);
7778
const int64_t expected_max_profit1 = 47;
@@ -86,15 +87,15 @@ static void test() {
8687
31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
8788
41, 42, 43, 44, 45, 46, 47, 48, 49, 50};
8889

89-
const int64_t max_profit2=
90+
const int64_t max_profit2 =
9091
dynamic_programming::cut_rod::maxProfitByCuttingRod(price2, n2);
9192
const int32_t expected_max_profit2 = 90;
9293
assert(max_profit2 == expected_max_profit2);
9394
std::cout << "Maximum profit with " << n2 << " inch road is " << max_profit2
9495
<< std::endl;
95-
// Test 3
96-
const int16_t n3 = 5; // size of rod
97-
std::array<int32_t, n3> price3 = {2,9,17,23,45}; // price array
96+
// Test 3
97+
const int16_t n3 = 5; // size of rod
98+
std::array<int32_t, n3> price3 = {2, 9, 17, 23, 45}; // price array
9899
const int64_t max_profit3 =
99100
dynamic_programming::cut_rod::maxProfitByCuttingRod(price3, n3);
100101
const int64_t expected_max_profit3 = 45;

‎dynamic_programming/edit_distance.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include <iostream>
1515
#include <string>
16+
#include <vector>
1617
using namespace std;
1718

1819
int min(int x, int y, int z) { return min(min(x, y), z); }
@@ -46,7 +47,7 @@ int editDist(string str1, string str2, int m, int n) {
4647
*/
4748
int editDistDP(string str1, string str2, int m, int n) {
4849
// Create Table for SubProblems
49-
int dp[m + 1][n + 1];
50+
std::vector<std::vector<int> > dp(m + 1, std::vector<int>(n + 1));
5051

5152
// Fill d[][] in bottom up manner
5253
for (int i = 0; i <= m; i++) {

‎dynamic_programming/egg_dropping_puzzle.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
#include <climits>
66
#include <iostream>
7+
#include <vector>
8+
79
using namespace std;
810

911
int eggDrop(int n, int k) {
10-
int eggFloor[n + 1][k + 1];
12+
std::vector<std::vector<int> > eggFloor(n + 1, std::vector<int>(k + 1));
13+
1114
int result;
1215

1316
for (int i = 1; i <= n; i++) {

‎dynamic_programming/floyd_warshall.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <climits>
2+
#include <cstddef>
23
#include <iostream>
3-
#include <string>
4+
#include <vector>
45

56
using std::cin;
67
using std::cout;
@@ -24,7 +25,9 @@ class Graph {
2425
}
2526

2627
~Graph() {
27-
for (int i = 0; i < vertexNum; i++) delete[] edges[i];
28+
for (int i = 0; i < vertexNum; i++) {
29+
delete[] edges[i];
30+
}
2831
delete[] edges;
2932
}
3033

@@ -35,7 +38,7 @@ class Graph {
3538
};
3639

3740
// Utility function to print distances
38-
void print(int dist[], int V) {
41+
void print(const std::vector<int>& dist, int V) {
3942
cout << "\nThe Distance matrix for Floyd - Warshall" << endl;
4043
for (int i = 0; i < V; i++) {
4144
for (int j = 0; j < V; j++) {
@@ -52,8 +55,8 @@ void print(int dist[], int V) {
5255
// The main function that finds the shortest path from a vertex
5356
// to all other vertices using Floyd-Warshall Algorithm.
5457
void FloydWarshall(Graph graph) {
55-
int V = graph.vertexNum;
56-
int dist[V][V];
58+
std::size_t V = graph.vertexNum;
59+
std::vector<std::vector<int> > dist(V, std::vector<int>(V));
5760

5861
// Initialise distance array
5962
for (int i = 0; i < V; i++)
@@ -76,7 +79,7 @@ void FloydWarshall(Graph graph) {
7679
dist[i][j] = dist[i][k] + dist[k][j];
7780

7881
// Convert 2d array to 1d array for print
79-
int dist1d[V * V];
82+
std::vector<int> dist1d(V * V);
8083
for (int i = 0; i < V; i++)
8184
for (int j = 0; j < V; j++) dist1d[i * V + j] = dist[i][j];
8285

‎dynamic_programming/house_robber.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
#include <cassert> /// for assert
1313
#include <climits> /// for std::max
14+
#include <cstdint> /// for std::uint32_t
1415
#include <iostream> /// for io operations
1516
#include <vector> /// for std::vector
16-
1717
/**
1818
* @namespace dynamic_programming
1919
* @brief Dynamic Programming algorithms

‎dynamic_programming/kadane.cpp

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,74 @@
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>
121
#include <climits>
222
#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
1450
}
15-
return max_so_far;
51+
return max_sum; // returning the value of max_sum
1652
}
53+
} // namespace kadane
54+
} // namespace dynamic_programming
1755

56+
/**
57+
* @brief Main function
58+
* @returns 0 on exit
59+
*/
1860
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];
2568
}
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+
2873
return 0;
2974
}

‎dynamic_programming/kadane2.cpp

Lines changed: 0 additions & 74 deletions
This file was deleted.

‎dynamic_programming/longest_common_subsequence.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Longest common subsequence - Dynamic Programming
22
#include <iostream>
3+
#include <vector>
34
using namespace std;
45

56
void Print(int trace[20][20], int m, int n, string a) {
@@ -18,7 +19,7 @@ void Print(int trace[20][20], int m, int n, string a) {
1819

1920
int lcs(string a, string b) {
2021
int m = a.length(), n = b.length();
21-
int res[m + 1][n + 1];
22+
std::vector<std::vector<int> > res(m + 1, std::vector<int>(n + 1));
2223
int trace[20][20];
2324

2425
// fills up the arrays with zeros.

‎dynamic_programming/longest_increasing_subsequence.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <cassert> /// for assert
2323
#include <climits> /// for std::max
24+
#include <cstdint> /// for std::uint64_t
2425
#include <iostream> /// for IO operations
2526
#include <vector> /// for std::vector
2627

‎dynamic_programming/longest_increasing_subsequence_(nlogn).cpp renamed to ‎dynamic_programming/longest_increasing_subsequence_nlogn.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
// tested on : https://cses.fi/problemset/task/1145/
44

55
#include <iostream>
6+
#include <set>
7+
#include <vector>
68

79
using namespace std;
8-
int LIS(int arr[], int n) {
10+
int LIS(const std::vector<int>& arr, int n) {
911
set<int> active; // The current built LIS.
1012
active.insert(arr[0]);
1113
// Loop through every element.
@@ -31,7 +33,7 @@ int main(int argc, char const* argv[]) {
3133
int n;
3234
cout << "Enter size of array: ";
3335
cin >> n;
34-
int a[n];
36+
std::vector<int> a(n);
3537
cout << "Enter array elements: ";
3638
for (int i = 0; i < n; ++i) {
3739
cin >> a[i];
Lines changed: 76 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/**
22
* @file
3-
* @brief Implementation of [Minimum Edit Distance](https://en.wikipedia.org/wiki/Edit_distance) using Dynamic Programing
3+
* @brief Implementation of [Minimum Edit
4+
* Distance](https://en.wikipedia.org/wiki/Edit_distance) using Dynamic
5+
* Programing
46
*
57
* @details
68
*
@@ -32,9 +34,11 @@
3234
* @author [Nirjas Jakilim](github.com/nirzak)
3335
*/
3436

35-
#include <cassert> /// for assert
36-
#include <iostream> /// for IO operations
37+
#include <cassert> /// for assert
38+
#include <cstdint> /// for std::uint64_t
39+
#include <iostream> /// for IO operations
3740
#include <vector> /// for std::vector
41+
3842
/**
3943
* @namespace dynamic_programming
4044
* @brief Dynamic Programming algorithms
@@ -44,7 +48,8 @@ namespace dynamic_programming {
4448

4549
/**
4650
* @namespace Minimum Edit Distance
47-
* @brief Implementation of [Minimum Edit Distance](https://en.wikipedia.org/wiki/Edit_distance) algorithm
51+
* @brief Implementation of [Minimum Edit
52+
* Distance](https://en.wikipedia.org/wiki/Edit_distance) algorithm
4853
*/
4954

5055
namespace minimum_edit_distance {
@@ -61,15 +66,14 @@ namespace minimum_edit_distance {
6166
* @returns z if `z` is the minimum value
6267
*/
6368
uint64_t min(uint64_t x, uint64_t y, uint64_t z) {
64-
if (x <= y && x <= z) {
65-
return x; /// returns x, if x is the minimum value
66-
}
67-
if (y <= x && y <= z) {
68-
return y; /// returns y, if y is the minimum value
69-
}
70-
else {
71-
return z; /// returns z if z is the minimum value
72-
}
69+
if (x <= y && x <= z) {
70+
return x; /// returns x, if x is the minimum value
71+
}
72+
if (y <= x && y <= z) {
73+
return y; /// returns y, if y is the minimum value
74+
} else {
75+
return z; /// returns z if z is the minimum value
76+
}
7377
}
7478

7579
/**
@@ -85,42 +89,48 @@ uint64_t min(uint64_t x, uint64_t y, uint64_t z) {
8589
* @returns dp[m][n] the minimum cost of operations
8690
* needed to convert str1 to str2
8791
*/
88-
uint64_t editDistDP(std::string str1, std::string str2, uint64_t m, uint64_t n) {
89-
/// Create a table to store results of subproblems
90-
std::vector<std::vector<uint64_t>>dp(m+1, std::vector<uint64_t>(n+1)); /// creasting 2D vector dp to store the results of subproblems
92+
uint64_t editDistDP(std::string str1, std::string str2, uint64_t m,
93+
uint64_t n) {
94+
/// Create a table to store results of subproblems
95+
std::vector<std::vector<uint64_t>> dp(
96+
m + 1,
97+
std::vector<uint64_t>(
98+
n +
99+
1)); /// creasting 2D vector dp to store the results of subproblems
91100

92-
/// Fill d[][] in bottom up manner
93-
for (uint64_t i = 0; i <= m; i++) {
94-
for (uint64_t j = 0; j <= n; j++) {
95-
/// If first string is empty, only option is to
96-
/// insert all characters of second string
97-
if (i == 0) {
98-
dp[i][j] = j; /// Minimum operations = j
99-
}
101+
/// Fill d[][] in bottom up manner
102+
for (uint64_t i = 0; i <= m; i++) {
103+
for (uint64_t j = 0; j <= n; j++) {
104+
/// If first string is empty, only option is to
105+
/// insert all characters of second string
106+
if (i == 0) {
107+
dp[i][j] = j; /// Minimum operations = j
108+
}
100109

101-
/// If second string is empty, only option is to
102-
/// remove all characters of second string
103-
else if (j == 0) {
104-
dp[i][j] = i; /// Minimum operations = i
105-
}
110+
/// If second string is empty, only option is to
111+
/// remove all characters of second string
112+
else if (j == 0) {
113+
dp[i][j] = i; /// Minimum operations = i
114+
}
106115

107-
/// If last characters are same, ignore last char
108-
/// and recur for remaining string
109-
else if (str1[i - 1] == str2[j - 1]) {
110-
dp[i][j] = dp[i - 1][j - 1];
111-
}
116+
/// If last characters are same, ignore last char
117+
/// and recur for remaining string
118+
else if (str1[i - 1] == str2[j - 1]) {
119+
dp[i][j] = dp[i - 1][j - 1];
120+
}
112121

113-
/// If the last character is different, consider all
114-
/// possibilities and find the minimum
115-
else {
116-
dp[i][j] = 1 + min(dp[i][j - 1], // Insert
117-
dp[i - 1][j], // Remove
118-
dp[i - 1][j - 1]); // Replace
119-
}
122+
/// If the last character is different, consider all
123+
/// possibilities and find the minimum
124+
else {
125+
dp[i][j] = 1 + min(dp[i][j - 1], // Insert
126+
dp[i - 1][j], // Remove
127+
dp[i - 1][j - 1]); // Replace
128+
}
129+
}
120130
}
121-
}
122131

123-
return dp[m][n]; /// returning the minimum cost of operations needed to convert str1 to str2
132+
return dp[m][n]; /// returning the minimum cost of operations needed to
133+
/// convert str1 to str2
124134
}
125135
} // namespace minimum_edit_distance
126136
} // namespace dynamic_programming
@@ -130,25 +140,28 @@ uint64_t editDistDP(std::string str1, std::string str2, uint64_t m, uint64_t n)
130140
* @returns void
131141
*/
132142
static void test() {
133-
// 1st test
134-
std::string str1 = "INTENTION"; // Sample input of 1st string
135-
std::string str2 = "EXECUTION"; // Sample input of 2nd string
136-
uint64_t expected_output1 = 5; // Expected minimum cost
137-
uint64_t output1 = dynamic_programming::minimum_edit_distance::editDistDP(
138-
str1, str2, str1.length(), str2.length()); // calling the editDistDP function and storing the result on output1
139-
assert(output1 == expected_output1); // comparing the output with the expected output
140-
std::cout << "Minimum Number of Operations Required: " << output1
141-
<< std::endl;
143+
// 1st test
144+
std::string str1 = "INTENTION"; // Sample input of 1st string
145+
std::string str2 = "EXECUTION"; // Sample input of 2nd string
146+
uint64_t expected_output1 = 5; // Expected minimum cost
147+
uint64_t output1 = dynamic_programming::minimum_edit_distance::editDistDP(
148+
str1, str2, str1.length(),
149+
str2.length()); // calling the editDistDP function and storing the
150+
// result on output1
151+
assert(output1 ==
152+
expected_output1); // comparing the output with the expected output
153+
std::cout << "Minimum Number of Operations Required: " << output1
154+
<< std::endl;
142155

143-
// 2nd test
144-
std::string str3 = "SATURDAY";
145-
std::string str4 = "SUNDAY";
146-
uint64_t expected_output2 = 3;
147-
uint64_t output2 = dynamic_programming::minimum_edit_distance::editDistDP(
148-
str3, str4, str3.length(), str4.length());
149-
assert(output2 == expected_output2);
150-
std::cout << "Minimum Number of Operations Required: " << output2
151-
<< std::endl;
156+
// 2nd test
157+
std::string str3 = "SATURDAY";
158+
std::string str4 = "SUNDAY";
159+
uint64_t expected_output2 = 3;
160+
uint64_t output2 = dynamic_programming::minimum_edit_distance::editDistDP(
161+
str3, str4, str3.length(), str4.length());
162+
assert(output2 == expected_output2);
163+
std::cout << "Minimum Number of Operations Required: " << output2
164+
<< std::endl;
152165
}
153166

154167
/**
@@ -158,6 +171,6 @@ static void test() {
158171
* @returns 0 on exit
159172
*/
160173
int main(int argc, char *argv[]) {
161-
test(); // run self-test implementations
162-
return 0;
174+
test(); // run self-test implementations
175+
return 0;
163176
}

‎dynamic_programming/partition_problem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
*
2929
*******************************************************************************/
3030
#include <cassert> /// for assert
31+
#include <cstdint> /// for std::uint64_t
3132
#include <iostream> /// for IO Operations
3233
#include <numeric> /// for std::accumulate
3334
#include <vector> /// for std::vector
34-
3535
/******************************************************************************
3636
* @namespace dp
3737
* @brief Dynamic programming algorithms

‎dynamic_programming/unbounded_0_1_knapsack.cpp

Lines changed: 88 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
/**
22
* @file
33
* @brief Implementation of the Unbounded 0/1 Knapsack Problem
4-
*
5-
* @details
6-
* The Unbounded 0/1 Knapsack problem allows taking unlimited quantities of each item.
7-
* The goal is to maximize the total value without exceeding the given knapsack capacity.
8-
* Unlike the 0/1 knapsack, where each item can be taken only once, in this variation,
9-
* any item can be picked any number of times as long as the total weight stays within
10-
* the knapsack's capacity.
11-
*
12-
* Given a set of N items, each with a weight and a value, represented by the arrays
13-
* `wt` and `val` respectively, and a knapsack with a weight limit W, the task is to
14-
* fill the knapsack to maximize the total value.
154
*
16-
* @note weight and value of items is greater than zero
5+
* @details
6+
* The Unbounded 0/1 Knapsack problem allows taking unlimited quantities of each
7+
* item. The goal is to maximize the total value without exceeding the given
8+
* knapsack capacity. Unlike the 0/1 knapsack, where each item can be taken only
9+
* once, in this variation, any item can be picked any number of times as long
10+
* as the total weight stays within the knapsack's capacity.
11+
*
12+
* Given a set of N items, each with a weight and a value, represented by the
13+
* arrays `wt` and `val` respectively, and a knapsack with a weight limit W, the
14+
* task is to fill the knapsack to maximize the total value.
15+
*
16+
* @note weight and value of items is greater than zero
1717
*
1818
* ### Algorithm
19-
* The approach uses dynamic programming to build a solution iteratively.
20-
* A 2D array is used for memoization to store intermediate results, allowing
19+
* The approach uses dynamic programming to build a solution iteratively.
20+
* A 2D array is used for memoization to store intermediate results, allowing
2121
* the function to avoid redundant calculations.
22-
*
22+
*
2323
* @author [Sanskruti Yeole](https://github.com/yeolesanskruti)
2424
* @see dynamic_programming/0_1_knapsack.cpp
2525
*/
2626

27+
#include <cassert> // For using assert function to validate test cases
28+
#include <cstdint> // For fixed-width integer types like std::uint16_t
2729
#include <iostream> // Standard input-output stream
28-
#include <vector> // Standard library for using dynamic arrays (vectors)
29-
#include <cassert> // For using assert function to validate test cases
30-
#include <cstdint> // For fixed-width integer types like std::uint16_t
30+
#include <vector> // Standard library for using dynamic arrays (vectors)
3131

3232
/**
3333
* @namespace dynamic_programming
@@ -42,7 +42,7 @@ namespace dynamic_programming {
4242
namespace unbounded_knapsack {
4343

4444
/**
45-
* @brief Recursive function to calculate the maximum value obtainable using
45+
* @brief Recursive function to calculate the maximum value obtainable using
4646
* an unbounded knapsack approach.
4747
*
4848
* @param i Current index in the value and weight vectors.
@@ -52,27 +52,33 @@ namespace unbounded_knapsack {
5252
* @param wt Vector of weights corresponding to the items.
5353
* @note "wt" data type can be changed according to the size of the input.
5454
* @param dp 2D vector for memoization to avoid redundant calculations.
55-
* @return The maximum value that can be obtained for the given index and capacity.
55+
* @return The maximum value that can be obtained for the given index and
56+
* capacity.
5657
*/
57-
std::uint16_t KnapSackFilling(std::uint16_t i, std::uint16_t W,
58-
const std::vector<std::uint16_t>& val,
59-
const std::vector<std::uint16_t>& wt,
60-
std::vector<std::vector<int>>& dp) {
58+
std::uint16_t KnapSackFilling(std::uint16_t i, std::uint16_t W,
59+
const std::vector<std::uint16_t>& val,
60+
const std::vector<std::uint16_t>& wt,
61+
std::vector<std::vector<int>>& dp) {
6162
if (i == 0) {
6263
if (wt[0] <= W) {
63-
return (W / wt[0]) * val[0]; // Take as many of the first item as possible
64+
return (W / wt[0]) *
65+
val[0]; // Take as many of the first item as possible
6466
} else {
65-
return 0; // Can't take the first item
67+
return 0; // Can't take the first item
6668
}
6769
}
68-
if (dp[i][W] != -1) return dp[i][W]; // Return result if available
70+
if (dp[i][W] != -1)
71+
return dp[i][W]; // Return result if available
6972

70-
int nottake = KnapSackFilling(i - 1, W, val, wt, dp); // Value without taking item i
73+
int nottake =
74+
KnapSackFilling(i - 1, W, val, wt, dp); // Value without taking item i
7175
int take = 0;
7276
if (W >= wt[i]) {
73-
take = val[i] + KnapSackFilling(i, W - wt[i], val, wt, dp); // Value taking item i
77+
take = val[i] + KnapSackFilling(i, W - wt[i], val, wt,
78+
dp); // Value taking item i
7479
}
75-
return dp[i][W] = std::max(take, nottake); // Store and return the maximum value
80+
return dp[i][W] =
81+
std::max(take, nottake); // Store and return the maximum value
7682
}
7783

7884
/**
@@ -84,68 +90,84 @@ std::uint16_t KnapSackFilling(std::uint16_t i, std::uint16_t W,
8490
* @param wt Vector of weights corresponding to the items.
8591
* @return The maximum value that can be obtained for the given capacity.
8692
*/
87-
std::uint16_t unboundedKnapsack(std::uint16_t N, std::uint16_t W,
88-
const std::vector<std::uint16_t>& val,
89-
const std::vector<std::uint16_t>& wt) {
90-
if(N==0)return 0; // Expect 0 since no items
91-
std::vector<std::vector<int>> dp(N, std::vector<int>(W + 1, -1)); // Initialize memoization table
92-
return KnapSackFilling(N - 1, W, val, wt, dp); // Start the calculation
93+
std::uint16_t unboundedKnapsack(std::uint16_t N, std::uint16_t W,
94+
const std::vector<std::uint16_t>& val,
95+
const std::vector<std::uint16_t>& wt) {
96+
if (N == 0)
97+
return 0; // Expect 0 since no items
98+
std::vector<std::vector<int>> dp(
99+
N, std::vector<int>(W + 1, -1)); // Initialize memoization table
100+
return KnapSackFilling(N - 1, W, val, wt, dp); // Start the calculation
93101
}
94102

95-
} // unbounded_knapsack
103+
} // namespace unbounded_knapsack
96104

97-
} // dynamic_programming
105+
} // namespace dynamic_programming
98106

99107
/**
100108
* @brief self test implementation
101109
* @return void
102110
*/
103111
static void tests() {
104112
// Test Case 1
105-
std::uint16_t N1 = 4; // Number of items
106-
std::vector<std::uint16_t> wt1 = {1, 3, 4, 5}; // Weights of the items
107-
std::vector<std::uint16_t> val1 = {6, 1, 7, 7}; // Values of the items
108-
std::uint16_t W1 = 8; // Maximum capacity of the knapsack
113+
std::uint16_t N1 = 4; // Number of items
114+
std::vector<std::uint16_t> wt1 = {1, 3, 4, 5}; // Weights of the items
115+
std::vector<std::uint16_t> val1 = {6, 1, 7, 7}; // Values of the items
116+
std::uint16_t W1 = 8; // Maximum capacity of the knapsack
109117
// Test the function and assert the expected output
110-
assert(unboundedKnapsack(N1, W1, val1, wt1) == 48);
111-
std::cout << "Maximum Knapsack value " << unboundedKnapsack(N1, W1, val1, wt1) << std::endl;
118+
assert(dynamic_programming::unbounded_knapsack::unboundedKnapsack(
119+
N1, W1, val1, wt1) == 48);
120+
std::cout << "Maximum Knapsack value "
121+
<< dynamic_programming::unbounded_knapsack::unboundedKnapsack(
122+
N1, W1, val1, wt1)
123+
<< std::endl;
112124

113125
// Test Case 2
114-
std::uint16_t N2 = 3; // Number of items
115-
std::vector<std::uint16_t> wt2 = {10, 20, 30}; // Weights of the items
116-
std::vector<std::uint16_t> val2 = {60, 100, 120}; // Values of the items
117-
std::uint16_t W2 = 5; // Maximum capacity of the knapsack
126+
std::uint16_t N2 = 3; // Number of items
127+
std::vector<std::uint16_t> wt2 = {10, 20, 30}; // Weights of the items
128+
std::vector<std::uint16_t> val2 = {60, 100, 120}; // Values of the items
129+
std::uint16_t W2 = 5; // Maximum capacity of the knapsack
118130
// Test the function and assert the expected output
119-
assert(unboundedKnapsack(N2, W2, val2, wt2) == 0);
120-
std::cout << "Maximum Knapsack value " << unboundedKnapsack(N2, W2, val2, wt2) << std::endl;
131+
assert(dynamic_programming::unbounded_knapsack::unboundedKnapsack(
132+
N2, W2, val2, wt2) == 0);
133+
std::cout << "Maximum Knapsack value "
134+
<< dynamic_programming::unbounded_knapsack::unboundedKnapsack(
135+
N2, W2, val2, wt2)
136+
<< std::endl;
121137

122138
// Test Case 3
123-
std::uint16_t N3 = 3; // Number of items
124-
std::vector<std::uint16_t> wt3 = {2, 4, 6}; // Weights of the items
125-
std::vector<std::uint16_t> val3 = {5, 11, 13};// Values of the items
126-
std::uint16_t W3 = 27;// Maximum capacity of the knapsack
139+
std::uint16_t N3 = 3; // Number of items
140+
std::vector<std::uint16_t> wt3 = {2, 4, 6}; // Weights of the items
141+
std::vector<std::uint16_t> val3 = {5, 11, 13}; // Values of the items
142+
std::uint16_t W3 = 27; // Maximum capacity of the knapsack
127143
// Test the function and assert the expected output
128-
assert(unboundedKnapsack(N3, W3, val3, wt3) == 27);
129-
std::cout << "Maximum Knapsack value " << unboundedKnapsack(N3, W3, val3, wt3) << std::endl;
144+
assert(dynamic_programming::unbounded_knapsack::unboundedKnapsack(
145+
N3, W3, val3, wt3) == 27);
146+
std::cout << "Maximum Knapsack value "
147+
<< dynamic_programming::unbounded_knapsack::unboundedKnapsack(
148+
N3, W3, val3, wt3)
149+
<< std::endl;
130150

131151
// Test Case 4
132-
std::uint16_t N4 = 0; // Number of items
133-
std::vector<std::uint16_t> wt4 = {}; // Weights of the items
134-
std::vector<std::uint16_t> val4 = {}; // Values of the items
135-
std::uint16_t W4 = 10; // Maximum capacity of the knapsack
136-
assert(unboundedKnapsack(N4, W4, val4, wt4) == 0);
137-
std::cout << "Maximum Knapsack value for empty arrays: " << unboundedKnapsack(N4, W4, val4, wt4) << std::endl;
138-
139-
std::cout << "All test cases passed!" << std::endl;
152+
std::uint16_t N4 = 0; // Number of items
153+
std::vector<std::uint16_t> wt4 = {}; // Weights of the items
154+
std::vector<std::uint16_t> val4 = {}; // Values of the items
155+
std::uint16_t W4 = 10; // Maximum capacity of the knapsack
156+
assert(dynamic_programming::unbounded_knapsack::unboundedKnapsack(
157+
N4, W4, val4, wt4) == 0);
158+
std::cout << "Maximum Knapsack value for empty arrays: "
159+
<< dynamic_programming::unbounded_knapsack::unboundedKnapsack(
160+
N4, W4, val4, wt4)
161+
<< std::endl;
140162

163+
std::cout << "All test cases passed!" << std::endl;
141164
}
142165

143166
/**
144167
* @brief main function
145168
* @return 0 on successful exit
146169
*/
147170
int main() {
148-
tests(); // Run self test implementation
171+
tests(); // Run self test implementation
149172
return 0;
150173
}
151-

‎dynamic_programming/word_break.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ bool check(const std::string &s, const std::unordered_set<std::string> &strSet,
105105
// if the prefix till current position is present in the dictionary
106106
// and the remaining substring can also be segmented legally, then
107107
// set solution at position pos in the memo, and return true
108-
if (exists(wordTillNow, strSet) and check(s, strSet, i + 1, dp)) {
108+
if (exists(wordTillNow, strSet) && check(s, strSet, i + 1, dp)) {
109109
dp->at(pos) = 1;
110110
return true;
111111
}

‎greedy_algorithms/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
2+
# with full pathname. RELATIVE may makes it easier to extract an executable name
3+
# automatically.
4+
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp )
5+
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
6+
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
7+
foreach( testsourcefile ${APP_SOURCES} )
8+
# I used a simple string replace, to cut off .cpp.
9+
string( REPLACE ".cpp" "" testname ${testsourcefile} )
10+
add_executable( ${testname} ${testsourcefile} )
11+
12+
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX)
13+
if(OpenMP_CXX_FOUND)
14+
target_link_libraries(${testname} OpenMP::OpenMP_CXX)
15+
endif()
16+
install(TARGETS ${testname} DESTINATION "bin/greedy_algorithms")
17+
18+
endforeach( testsourcefile ${APP_SOURCES} )

‎greedy_algorithms/knapsack.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ int main() {
4444
cout << "\n Enter the number of Items : ";
4545
int n;
4646
cin >> n;
47-
Item itemArray[n];
47+
Item *itemArray = new Item[n];
4848
for (int i = 0; i < n; i++) {
4949
cout << "\nEnter the weight and profit of item " << i + 1 << " : ";
5050
cin >> itemArray[i].weight;
@@ -73,6 +73,6 @@ int main() {
7373
}
7474

7575
cout << "\nMax Profit : " << maxProfit;
76-
76+
delete[] itemArray;
7777
return 0;
7878
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
2+
# with full pathname. RELATIVE may makes it easier to extract an executable name
3+
# automatically.
4+
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp )
5+
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
6+
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
7+
foreach( testsourcefile ${APP_SOURCES} )
8+
# I used a simple string replace, to cut off .cpp.
9+
string( REPLACE ".cpp" "" testname ${testsourcefile} )
10+
add_executable( ${testname} ${testsourcefile} )
11+
12+
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX)
13+
install(TARGETS ${testname} DESTINATION "bin/operations_on_datastructures")
14+
15+
endforeach( testsourcefile ${APP_SOURCES} )

‎operations_on_datastructures/circular_queue_using_array.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <iostream>
2-
using namespace std;
2+
using std::cin;
3+
using std::cout;
34

45
int queue[10];
56
int front = 0;

‎operations_on_datastructures/trie_multiple_search.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @file
3-
* @brief [Trie datastructure](https://iq.opengenus.org/autocomplete-using-trie-data-structure/)
3+
* @brief [Trie
4+
* datastructure](https://iq.opengenus.org/autocomplete-using-trie-data-structure/)
45
* with search variants
56
* @details
67
* This provides multiple variants of search functions
@@ -12,6 +13,7 @@
1213
#include <algorithm> /// for std::count
1314
#include <cassert> /// for assert
1415
#include <cctype> /// for tolower
16+
#include <cstdint> /// for std::uint32_t
1517
#include <cstring> /// for string operations
1618
#include <iostream> /// for IO Operations
1719
#include <queue> /// for std::priority_queue
@@ -23,7 +25,8 @@
2325
namespace operations_on_datastructures {
2426
/**
2527
* @namespace trie_operations
26-
* @brief Functions for [Trie datastructure](https://iq.opengenus.org/autocomplete-using-trie-data-structure/)
28+
* @brief Functions for [Trie
29+
* datastructure](https://iq.opengenus.org/autocomplete-using-trie-data-structure/)
2730
* implementation
2831
*/
2932
namespace trie_operations {

‎others/fast_integer_input.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
void fastinput(int *number) {
1212
// variable to indicate sign of input integer
1313
bool negative = false;
14-
register int c;
14+
int c;
1515
*number = 0;
1616

1717
// extract current character from buffer

‎range_queries/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
2+
# with full pathname. RELATIVE may makes it easier to extract an executable name
3+
# automatically.
4+
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp )
5+
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
6+
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
7+
foreach( testsourcefile ${APP_SOURCES} )
8+
# I used a simple string replace, to cut off .cpp.
9+
string( REPLACE ".cpp" "" testname ${testsourcefile} )
10+
add_executable( ${testname} ${testsourcefile} )
11+
12+
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX)
13+
if(OpenMP_CXX_FOUND)
14+
target_link_libraries(${testname} OpenMP::OpenMP_CXX)
15+
endif()
16+
install(TARGETS ${testname} DESTINATION "bin/range_queries")
17+
18+
endforeach( testsourcefile ${APP_SOURCES} )

‎range_queries/mo.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
#include <algorithm>
2+
#include <cmath>
13
#include <iostream>
4+
25
using namespace std;
36
const int N = 1e6 + 5;
47
int a[N], bucket[N], cnt[N];

‎range_queries/segtree.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <cassert> /// for assert
2323
#include <cmath> /// for log2
24+
#include <cstdint> /// for std::uint64_t
2425
#include <iostream> /// for IO operations
2526
#include <vector> /// for std::vector
2627

0 commit comments

Comments
 (0)
Please sign in to comment.