Skip to content

Commit

Permalink
feat: Add ncr mod p code (TheAlgorithms#1325)
Browse files Browse the repository at this point in the history
* feat: Add ncr mod p code (TheAlgorithms#1323)

* Update math/ncr_modulo_p.cpp

Co-authored-by: David Leal <[email protected]>

* Added all functions inside a class + added more asserts

* updating DIRECTORY.md

* clang-format and clang-tidy fixes for f6df24a

* Replace int64_t to uint64_t + add namespace + detailed documentation

* clang-format and clang-tidy fixes for e09a057

* Add extra namespace + add const& in function arguments

* clang-format and clang-tidy fixes for 8111f88

* Update ncr_modulo_p.cpp

* clang-format and clang-tidy fixes for 2ad2f72

* Update math/ncr_modulo_p.cpp

Co-authored-by: David Leal <[email protected]>

* Update math/ncr_modulo_p.cpp

Co-authored-by: David Leal <[email protected]>

* Update math/ncr_modulo_p.cpp

Co-authored-by: David Leal <[email protected]>

* clang-format and clang-tidy fixes for 5b69ba5

* updating DIRECTORY.md

* clang-format and clang-tidy fixes for a8401d4

Co-authored-by: David Leal <[email protected]>
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 22, 2020
1 parent c8ce6f4 commit 67e26cf
Show file tree
Hide file tree
Showing 19 changed files with 1,078 additions and 864 deletions.
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
* [Modular Exponentiation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/modular_exponentiation.cpp)
* [Modular Inverse Fermat Little Theorem](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/modular_inverse_fermat_little_theorem.cpp)
* [N Choose R](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/n_choose_r.cpp)
* [Ncr Modulo P](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/ncr_modulo_p.cpp)
* [Number Of Positive Divisors](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/number_of_positive_divisors.cpp)
* [Power For Huge Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/power_for_huge_numbers.cpp)
* [Prime Factorization](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/prime_factorization.cpp)
Expand Down
20 changes: 11 additions & 9 deletions data_structures/list_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,27 @@
* @todo Add documentation
* @warning The sorting algorithm is erroneous
*/
#include <iostream>
#include <array>
#include <iostream>

struct list {
std::array<int, 50> data{};
int top = 0;
bool isSorted = false;

int BinarySearch(const std::array<int, 50>& dataArr, int first, int last, int x) {
int BinarySearch(const std::array<int, 50>& dataArr, int first, int last,
int x) {
if (last < first) {
return -1;
}
int mid = (first + last) / 2;
if (dataArr[mid] == x)
if (dataArr[mid] == x) {
return mid;
else if (x < dataArr[mid])
} else if (x < dataArr[mid]) {
return (BinarySearch(dataArr, first, mid - 1, x));
else if (x > dataArr[mid])
} else if (x > dataArr[mid]) {
return (BinarySearch(dataArr, mid + 1, last, x));
}

std::cerr << __func__ << ":" << __LINE__ << ": Undefined condition\n";
return -1;
Expand All @@ -38,7 +40,7 @@ struct list {
}

int Search(int x) {
int pos;
int pos = 0;

if (isSorted) {
pos = BinarySearch(data, 0, top - 1, x);
Expand All @@ -55,7 +57,7 @@ struct list {
}

void Sort() {
int i, j, pos=0;
int i = 0, j = 0, pos = 0;
for (i = 0; i < top; i++) {
int min = data[i];
for (j = i + 1; j < top; j++) {
Expand Down Expand Up @@ -119,8 +121,8 @@ struct list {

int main() {
list L;
int choice;
int x;
int choice = 0;
int x = 0;
do {
// Choices for operations on the list_array.
std::cout << "\n0.Exit";
Expand Down
42 changes: 22 additions & 20 deletions data_structures/queue_using_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@
* values, which can be added to the end line (enqueue), removed from
* head of line (dequeue) and displayed.
* ### Algorithm
* Values can be added by increasing the `rear` variable by 1 (which points to
* the end of the array), then assigning new value to `rear`'s element of the array.
*
* Values can be removed by increasing the `front` variable by 1 (which points to
* the first of the array), so it cannot reached any more.
*
* Values can be added by increasing the `rear` variable by 1 (which points to
* the end of the array), then assigning new value to `rear`'s element of the
* array.
*
* Values can be removed by increasing the `front` variable by 1 (which points
* to the first of the array), so it cannot reached any more.
*
* @author [Pooja](https://github.com/pooja-git11)
* @author [Farbod Ahmadian](https://github.com/farbodahm)
*/
#include <iostream> /// for io operations
#include <array> /// for std::array
#include <array> /// for std::array
#include <iostream> /// for io operations

constexpr uint16_t max_size{10}; ///< Maximum size of the queue
constexpr uint16_t max_size{10}; ///< Maximum size of the queue

/**
* @namespace data_structures
Expand All @@ -30,29 +31,31 @@ namespace data_structures {
/**
* @namespace queue_using_array
* @brief Functions for [Queue using Array]
* (https://www.geeksforgeeks.org/array-implementation-of-queue-simple/) implementation.
* (https://www.geeksforgeeks.org/array-implementation-of-queue-simple/)
* implementation.
*/
namespace queue_using_array {

/**
* @brief Queue_Array class containing the main data and also index of head and tail of the array.
* @brief Queue_Array class containing the main data and also index of head and
* tail of the array.
*/
class Queue_Array {
public:
public:
void enqueue(const int16_t&); ///< Add element to the first of the queue
int dequeue(); ///< Delete element from back of the queue
void display() const; ///< Show all saved data
private:
int8_t front{-1}; ///< Index of head of the array
int8_t rear{-1}; ///< Index of tail of the array
std::array<int16_t, max_size> arr; ///< All stored data
int dequeue(); ///< Delete element from back of the queue
void display() const; ///< Show all saved data
private:
int8_t front{-1}; ///< Index of head of the array
int8_t rear{-1}; ///< Index of tail of the array
std::array<int16_t, max_size> arr{}; ///< All stored data
};

/**
* @brief Adds new element to the end of the queue
* @param ele to be added to the end of the queue
*/
void Queue_Array::enqueue(const int16_t& ele ) {
void Queue_Array::enqueue(const int16_t& ele) {
if (rear == arr.size() - 1) {
std::cout << "\nStack is full";
} else if (front == -1 && rear == -1) {
Expand Down Expand Up @@ -98,7 +101,6 @@ void Queue_Array::display() const {
} // namespace queue_using_array
} // namespace data_structures


/**
* @brief Main function
* @details
Expand Down
3 changes: 2 additions & 1 deletion dynamic_programming/0_1_knapsack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ int maxKnapsackValue(const int capacity, const std::array<int, n> &weight,
int profit2 = maxValue[i - 1][j];

maxValue[i][j] = std::max(profit1, profit2);
} else
} else {
// as weight of current item is greater than allowed weight, so
// maxProfit will be profit obtained by excluding current item.
maxValue[i][j] = maxValue[i - 1][j];
}
}
}

Expand Down
81 changes: 43 additions & 38 deletions dynamic_programming/coin_change_topdown.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* @file
* @brief [Minimum coins](https://leetcode.com/problems/coin-change/) change problem is a problem used to find the minimum number of
* coins required to completely reach a target amount.
* @brief [Minimum coins](https://leetcode.com/problems/coin-change/) change
* problem is a problem used to find the minimum number of coins required to
* completely reach a target amount.
*
* @details
* This problem can be solved using 2 methods:
Expand All @@ -17,10 +18,10 @@
* @author [Divyansh Kushwaha](https://github.com/webdesignbydivyansh)
*/

#include <iostream> // for io operations
#include <vector> // for std::vector
#include <cassert> // for assert
#include <climits> // for INT_MAX
#include <cassert> // for assert
#include <climits> // for INT_MAX
#include <iostream> // for io operations
#include <vector> // for std::vector

/**
* @namespace dynamic_programming
Expand All @@ -29,36 +30,39 @@
namespace dynamic_programming {
/**
* @namespace mincoins_topdown
* @brief Functions for [minimum coin exchange](https://leetcode.com/problems/coin-change/) problem
* @brief Functions for [minimum coin
* exchange](https://leetcode.com/problems/coin-change/) problem
*/
namespace mincoins_topdown {
/**
* @brief This implementation is for finding minimum number of coins .
* @param T template-type to use any kind of value
* @param n amount to be reached
* @param coins vector of coins
* @param t deontes the number of coins
* @param dp initilised to 0
* @returns minimum number of coins
*/
template<typename T>
int64_t mincoins(const T &n, const std::vector<T> &coins, const int16_t &t, std::vector<T> dp){
if(n==0){
return 0;
}
if(dp[n]!=0){
return dp[n];
}
int ans=INT_MAX; //variable to store min coins
for(int i=0;i<t;i++){
if(n-coins[i]>=0){ //if after subtracting the current denomination is it greater than 0 or not
int sub=mincoins(n-coins[i],coins,t,dp);
ans=std::min(ans,sub+1);
}
/**
* @brief This implementation is for finding minimum number of coins .
* @param T template-type to use any kind of value
* @param n amount to be reached
* @param coins vector of coins
* @param t deontes the number of coins
* @param dp initilised to 0
* @returns minimum number of coins
*/
template <typename T>
int64_t mincoins(const T &n, const std::vector<T> &coins, const int16_t &t,
std::vector<T> dp) {
if (n == 0) {
return 0;
}
if (dp[n] != 0) {
return dp[n];
}
int ans = INT_MAX; // variable to store min coins
for (int i = 0; i < t; i++) {
if (n - coins[i] >= 0) { // if after subtracting the current
// denomination is it greater than 0 or not
int sub = mincoins(n - coins[i], coins, t, dp);
ans = std::min(ans, sub + 1);
}
dp[n]=ans;
return dp[n]; //returns minimum number of coins
}
dp[n] = ans;
return dp[n]; // returns minimum number of coins
}

} // namespace mincoins_topdown
} // namespace dynamic_programming
Expand All @@ -70,12 +74,13 @@ namespace mincoins_topdown {
static void test() {
// example 1: number of coins=3 and minimum coins required=3(7,7,1)
const int64_t n1 = 15;
const int8_t t1=3, a1=0;
const int8_t t1 = 3, a1 = 0;
std::cout << "\nTest 1...";
std::vector<int64_t> arr1 {1,7,10};
std::vector<int64_t> dp1 (n1+1);
fill(dp1.begin(),dp1.end(),a1);
assert(dynamic_programming::mincoins_topdown::mincoins(n1, arr1, t1, dp1)==3);
std::vector<int64_t> arr1{1, 7, 10};
std::vector<int64_t> dp1(n1 + 1);
fill(dp1.begin(), dp1.end(), a1);
assert(dynamic_programming::mincoins_topdown::mincoins(n1, arr1, t1, dp1) ==
3);
std::cout << "Passed\n";
}

Expand All @@ -84,6 +89,6 @@ static void test() {
* @returns 0 on exit
*/
int main() {
test(); // execute the test
test(); // execute the test
return 0;
}
Loading

0 comments on commit 67e26cf

Please sign in to comment.