Skip to content

Commit e697904

Browse files
authored
Merge branch 'master' into patch-8
2 parents ef7f2ff + 7bab516 commit e697904

File tree

10 files changed

+1115
-16
lines changed

10 files changed

+1115
-16
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ bool func(int param1, T param2) {
113113
* @returns void
114114
*/
115115
static void test() {
116-
/* desciptions of the following test */
116+
/* descriptions of the following test */
117117
assert(func(...) == ...); // this ensures that the algorithm works as expected
118118

119119
// can have multiple checks

DIRECTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
* [Nqueen Print All Solutions](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/nqueen_print_all_solutions.cpp)
99
* [Rat Maze](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/rat_maze.cpp)
1010
* [Subarray Sum](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/subarray_sum.cpp)
11+
* [Subset Sum](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/subset_sum.cpp)
1112
* [Sudoku Solve](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/sudoku_solve.cpp)
1213

1314
## Bit Manipulation
15+
* [Count Of Set Bits](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/bit_manipulation/count_of_set_bits.cpp)
1416
* [Hamming Distance](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/bit_manipulation/hamming_distance.cpp)
1517

1618
## Ciphers
@@ -74,6 +76,7 @@
7476
* [Egg Dropping Puzzle](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/egg_dropping_puzzle.cpp)
7577
* [Fibonacci Bottom Up](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/fibonacci_bottom_up.cpp)
7678
* [Floyd Warshall](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/floyd_warshall.cpp)
79+
* [House Robber](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/house_robber.cpp)
7780
* [Kadane](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/kadane.cpp)
7881
* [Kadane2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/kadane2.cpp)
7982
* [Longest Common String](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/dynamic_programming/longest_common_string.cpp)
@@ -269,6 +272,7 @@
269272
* [Linear Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/search/linear_search.cpp)
270273
* [Median Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/search/median_search.cpp)
271274
* [Saddleback Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/search/saddleback_search.cpp)
275+
* [Sublist Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/search/sublist_search.cpp)
272276
* [Ternary Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/search/ternary_search.cpp)
273277
* [Text Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/search/text_search.cpp)
274278

@@ -299,6 +303,7 @@
299303
* [Quick Sort 3](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/quick_sort_3.cpp)
300304
* [Radix Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/radix_sort.cpp)
301305
* [Radix Sort2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/radix_sort2.cpp)
306+
* [Random Pivot Quick Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/random_pivot_quick_sort.cpp)
302307
* [Recursive Bubble Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/recursive_bubble_sort.cpp)
303308
* [Selection Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/selection_sort.cpp)
304309
* [Shell Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/shell_sort.cpp)
@@ -307,6 +312,7 @@
307312
* [Strand Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/strand_sort.cpp)
308313
* [Swap Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/swap_sort.cpp)
309314
* [Tim Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/tim_sort.cpp)
315+
* [Wave Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/wave_sort.cpp)
310316
* [Wiggle Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/wiggle_sort.cpp)
311317

312318
## Strings

backtracking/graph_coloring.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ namespace backtracking {
3030
*/
3131
template <size_t V>
3232
void printSolution(const std::array <int, V>& color) {
33-
std::cout << "Following are the assigned colors\n";
33+
std::cout << "Following are the assigned colors" << std::endl;
3434
for (auto &col : color) {
3535
std::cout << col;
3636
}
37-
std::cout << "\n";
37+
std::cout << std::endl;
3838
}
3939

4040
/** A utility function to check if the current color assignment is safe for

backtracking/subset_sum.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* @file
3+
* @brief Implementation of the [Subset
4+
* Sum](https://en.wikipedia.org/wiki/Subset_sum_problem) problem.
5+
* @details
6+
* We are given an array and a sum value. The algorithm finds all
7+
* the subsets of that array with sum equal to the given sum and return such
8+
* subsets count. This approach will have exponential time complexity.
9+
* @author [Swastika Gupta](https://github.com/Swastyy)
10+
*/
11+
12+
#include <cassert> /// for assert
13+
#include <iostream> /// for IO operations
14+
#include <vector> /// for std::vector
15+
16+
/**
17+
* @namespace backtracking
18+
* @brief Backtracking algorithms
19+
*/
20+
namespace backtracking {
21+
/**
22+
* @namespace Subsets
23+
* @brief Functions for the [Subset
24+
* Sum](https://en.wikipedia.org/wiki/Subset_sum_problem) problem.
25+
*/
26+
namespace subset_sum {
27+
/**
28+
* @brief The main function implements count of subsets
29+
* @param sum is the required sum of any subset
30+
* @param in_arr is the input array
31+
* @returns count of the number of subsets with required sum
32+
*/
33+
uint64_t number_of_subsets(int32_t sum, const std::vector<int32_t> &in_arr) {
34+
int32_t nelement = in_arr.size();
35+
uint64_t count_of_subset = 0;
36+
37+
for (int32_t i = 0; i < (1 << (nelement)); i++) {
38+
int32_t check = 0;
39+
for (int32_t j = 0; j < nelement; j++) {
40+
if (i & (1 << j)) {
41+
check += (in_arr[j]);
42+
}
43+
}
44+
if (check == sum) {
45+
count_of_subset++;
46+
}
47+
}
48+
return count_of_subset;
49+
}
50+
} // namespace subset_sum
51+
} // namespace backtracking
52+
53+
/**
54+
* @brief Test implementations
55+
* @returns void
56+
*/
57+
static void test() {
58+
// 1st test
59+
std::cout << "1st test ";
60+
std::vector<int32_t> array1 = {-7, -3, -2, 5, 8}; // input array
61+
assert(backtracking::subset_sum::number_of_subsets(0, array1) ==
62+
2); // first argument in subset_sum function is the required sum and
63+
// second is the input array
64+
std::cout << "passed" << std::endl;
65+
66+
// 2nd test
67+
std::cout << "2nd test ";
68+
std::vector<int32_t> array2 = {1, 2, 3, 3};
69+
assert(backtracking::subset_sum::number_of_subsets(6, array2) ==
70+
3); // here we are expecting 3 subsets which sum up to 6 i.e.
71+
// {(1,2,3),(1,2,3),(3,3)}
72+
std::cout << "passed" << std::endl;
73+
74+
// 3rd test
75+
std::cout << "3rd test ";
76+
std::vector<int32_t> array3 = {1, 1, 1, 1};
77+
assert(backtracking::subset_sum::number_of_subsets(1, array3) ==
78+
4); // here we are expecting 4 subsets which sum up to 1 i.e.
79+
// {(1),(1),(1),(1)}
80+
std::cout << "passed" << std::endl;
81+
82+
// 4th test
83+
std::cout << "4th test ";
84+
std::vector<int32_t> array4 = {3, 3, 3, 3};
85+
assert(backtracking::subset_sum::number_of_subsets(6, array4) ==
86+
6); // here we are expecting 6 subsets which sum up to 6 i.e.
87+
// {(3,3),(3,3),(3,3),(3,3),(3,3),(3,3)}
88+
std::cout << "passed" << std::endl;
89+
90+
// Test 5
91+
std::cout << "5th test ";
92+
std::vector<int32_t> array5 = {};
93+
assert(backtracking::subset_sum::number_of_subsets(6, array5) ==
94+
0); // here we are expecting 0 subsets which sum up to 6 i.e. we
95+
// cannot select anything from an empty array
96+
std::cout << "passed" << std::endl;
97+
}
98+
99+
/**
100+
* @brief Main function
101+
* @returns 0 on exit
102+
*/
103+
int main() {
104+
test(); // run self-test implementations
105+
return 0;
106+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* @file
3+
* @brief Implementation to [count sets
4+
* bits](https://www.geeksforgeeks.org/count-set-bits-in-an-integer/) in an
5+
* integer.
6+
*
7+
* @details
8+
* We are given an integer number. Let’s say, number. The task is to first
9+
* calculate the binary digit of a number and then calculate the total set bits
10+
* of a number.
11+
*
12+
* Set bits in a binary number is represented by 1. Whenever we calculate the
13+
* binary number of an integer value it is formed as the combination of 0’s and
14+
* 1’s. So digit 1 is known as a set bit in computer terms.
15+
* Time Complexity: O(log n)
16+
* Space complexity: O(1)
17+
* @author [Swastika Gupta](https://github.com/Swastyy)
18+
*/
19+
20+
#include <cassert> /// for assert
21+
#include <iostream> /// for io operations
22+
#include <vector> /// for std::vector
23+
24+
/**
25+
* @namespace bit_manipulation
26+
* @brief Bit manipulation algorithms
27+
*/
28+
namespace bit_manipulation {
29+
/**
30+
* @namespace count_of_set_bits
31+
* @brief Functions for the [count sets
32+
* bits](https://www.geeksforgeeks.org/count-set-bits-in-an-integer/)
33+
* implementation
34+
*/
35+
namespace count_of_set_bits {
36+
/**
37+
* @brief The main function implements set bit count
38+
* @param n is the number whose set bit will be counted
39+
* @returns the count of the number set bit in the binary representation of `n`
40+
*/
41+
std::uint64_t countSetBits(int n) {
42+
int count = 0; // "count" variable is used to count number of 1's in binary
43+
// representation of the number
44+
while (n != 0) {
45+
count += n & 1;
46+
n = n >> 1; // n=n/2
47+
}
48+
return count;
49+
}
50+
} // namespace count_of_set_bits
51+
} // namespace bit_manipulation
52+
53+
/**
54+
* @brief Self-test implementations
55+
* @returns void
56+
*/
57+
static void test() {
58+
// n = 4 return 1
59+
assert(bit_manipulation::count_of_set_bits::countSetBits(4) == 1);
60+
// n = 6 return 2
61+
assert(bit_manipulation::count_of_set_bits::countSetBits(6) == 2);
62+
// n = 13 return 3
63+
assert(bit_manipulation::count_of_set_bits::countSetBits(13) == 3);
64+
// n = 9 return 2
65+
assert(bit_manipulation::count_of_set_bits::countSetBits(9) == 2);
66+
// n = 15 return 4
67+
assert(bit_manipulation::count_of_set_bits::countSetBits(15) == 4);
68+
// n = 25 return 3
69+
assert(bit_manipulation::count_of_set_bits::countSetBits(25) == 3);
70+
std::cout << "All test cases successfully passed!" << std::endl;
71+
}
72+
73+
/**
74+
* @brief Main function
75+
* @returns 0 on exit
76+
*/
77+
int main() {
78+
test(); // run self-test implementations
79+
return 0;
80+
}

dynamic_programming/house_robber.cpp

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* @file
3+
* @brief Implementation of [House Robber
4+
* Problem](https://labuladong.gitbook.io/algo-en/i.-dynamic-programming/houserobber)
5+
* algorithm
6+
* @details
7+
* Solution of House robber problem uses a dynamic programming concept that
8+
* works in \f$O(n)\f$ time and works in \f$O(1)\f$ space.
9+
* @author [Swastika Gupta](https://github.com/Swastyy)
10+
*/
11+
12+
#include <cassert> /// for assert
13+
#include <climits> /// for std::max
14+
#include <iostream> /// for io operations
15+
#include <vector> /// for std::vector
16+
17+
/**
18+
* @namespace dynamic_programming
19+
* @brief Dynamic Programming algorithms
20+
*/
21+
namespace dynamic_programming {
22+
/**
23+
* @namespace house_robber
24+
* @brief Functions for the [House
25+
* Robber](https://labuladong.gitbook.io/algo-en/i.-dynamic-programming/houserobber)
26+
* algorithm
27+
*/
28+
namespace house_robber {
29+
/**
30+
* @brief The main function that implements the House Robber algorithm using
31+
* dynamic programming
32+
* @param money array containing money in the ith house
33+
* @param n size of array
34+
* @returns maximum amount of money that can be robbed
35+
*/
36+
std::uint32_t houseRobber(const std::vector<uint32_t> &money,
37+
const uint32_t &n) {
38+
if (n == 0) { // if there is no house
39+
return 0;
40+
}
41+
if (n == 1) { // if there is only one house
42+
return money[0];
43+
}
44+
if (n == 2) { // if there are two houses, one with the maximum amount of
45+
// money will be robbed
46+
return std::max(money[0], money[1]);
47+
}
48+
uint32_t max_value = 0; // contains maximum stolen value at the end
49+
uint32_t value1 = money[0];
50+
uint32_t value2 = std::max(money[0], money[1]);
51+
for (uint32_t i = 2; i < n; i++) {
52+
max_value = std::max(money[i] + value1, value2);
53+
value1 = value2;
54+
value2 = max_value;
55+
}
56+
57+
return max_value;
58+
}
59+
} // namespace house_robber
60+
} // namespace dynamic_programming
61+
62+
/**
63+
* @brief Self-test implementations
64+
* @returns void
65+
*/
66+
static void test() {
67+
// Test 1
68+
// [1, 2, 3, 1] return 4
69+
std::vector<uint32_t> array1 = {1, 2, 3, 1};
70+
std::cout << "Test 1... ";
71+
assert(
72+
dynamic_programming::house_robber::houseRobber(array1, array1.size()) ==
73+
4); // here the two non-adjacent houses that are robbed are first and
74+
// third with total sum money as 4
75+
std::cout << "passed" << std::endl;
76+
77+
// Test 2
78+
// [6, 7, 1, 3, 8, 2, 4] return 19
79+
std::vector<uint32_t> array2 = {6, 7, 1, 3, 8, 2, 4};
80+
std::cout << "Test 2... ";
81+
assert(
82+
dynamic_programming::house_robber::houseRobber(array2, array2.size()) ==
83+
19); // here the four non-adjacent houses that are robbed are first,
84+
// third, fifth and seventh with total sum money as 19
85+
std::cout << "passed" << std::endl;
86+
87+
// Test 3
88+
// [] return 0
89+
std::vector<uint32_t> array3 = {};
90+
std::cout << "Test 3... ";
91+
assert(
92+
dynamic_programming::house_robber::houseRobber(array3, array3.size()) ==
93+
0); // since there is no house no money can be robbed
94+
std::cout << "passed" << std::endl;
95+
96+
// Test 4
97+
// [2,7,9,3,1] return 12
98+
std::vector<uint32_t> array4 = {2, 7, 9, 3, 1};
99+
std::cout << "Test 4... ";
100+
assert(
101+
dynamic_programming::house_robber::houseRobber(array4, array4.size()) ==
102+
12); // here the three non-adjacent houses that are robbed are first,
103+
// third and fifth with total sum money as 12
104+
std::cout << "passed" << std::endl;
105+
}
106+
107+
/**
108+
* @brief Main function
109+
* @returns 0 on exit
110+
*/
111+
int main() {
112+
test(); // run self-test implementations
113+
return 0;
114+
}

0 commit comments

Comments
 (0)