Skip to content

Commit b9020ca

Browse files
Merge branch 'master' into master
2 parents cf43c33 + 37a9811 commit b9020ca

File tree

3 files changed

+204
-43
lines changed

3 files changed

+204
-43
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@
325325
* [Addition Rule](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/addition_rule.cpp)
326326
* [Bayes Theorem](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/bayes_theorem.cpp)
327327
* [Binomial Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/binomial_dist.cpp)
328+
* [Exponential Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/exponential_dist.cpp)
328329
* [Geometric Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/geometric_dist.cpp)
329330
* [Poisson Dist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/poisson_dist.cpp)
330331
* [Windowed Median](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/probability/windowed_median.cpp)
@@ -339,6 +340,7 @@
339340
* [Sparse Table](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/range_queries/sparse_table.cpp)
340341

341342
## Search
343+
* [Longest Increasing Subsequence Using Binary Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/search/Longest_Increasing_Subsequence_using_binary_search.cpp)
342344
* [Binary Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/search/binary_search.cpp)
343345
* [Exponential Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/search/exponential_search.cpp)
344346
* [Fibonacci Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/search/fibonacci_search.cpp)

math/modular_inverse_fermat_little_theorem.cpp

Lines changed: 85 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
* a^{m-2} &≡& a^{-1} \;\text{mod}\; m
3131
* \f}
3232
*
33-
* We will find the exponent using binary exponentiation. Such that the
34-
* algorithm works in \f$O(\log m)\f$ time.
33+
* We will find the exponent using binary exponentiation such that the
34+
* algorithm works in \f$O(\log n)\f$ time.
3535
*
3636
* Examples: -
3737
* * a = 3 and m = 7
@@ -43,56 +43,98 @@
4343
* (as \f$a\times a^{-1} = 1\f$)
4444
*/
4545

46-
#include <iostream>
47-
#include <vector>
46+
#include <cassert> /// for assert
47+
#include <cstdint> /// for std::int64_t
48+
#include <iostream> /// for IO implementations
4849

49-
/** Recursive function to calculate exponent in \f$O(\log n)\f$ using binary
50-
* exponent.
50+
/**
51+
* @namespace math
52+
* @brief Maths algorithms.
53+
*/
54+
namespace math {
55+
/**
56+
* @namespace modular_inverse_fermat
57+
* @brief Calculate modular inverse using Fermat's Little Theorem.
58+
*/
59+
namespace modular_inverse_fermat {
60+
/**
61+
* @brief Calculate exponent with modulo using binary exponentiation in \f$O(\log b)\f$ time.
62+
* @param a The base
63+
* @param b The exponent
64+
* @param m The modulo
65+
* @return The result of \f$a^{b} % m\f$
5166
*/
52-
int64_t binExpo(int64_t a, int64_t b, int64_t m) {
53-
a %= m;
54-
int64_t res = 1;
55-
while (b > 0) {
56-
if (b % 2) {
57-
res = res * a % m;
58-
}
59-
a = a * a % m;
60-
// Dividing b by 2 is similar to right shift.
61-
b >>= 1;
67+
std::int64_t binExpo(std::int64_t a, std::int64_t b, std::int64_t m) {
68+
a %= m;
69+
std::int64_t res = 1;
70+
while (b > 0) {
71+
if (b % 2 != 0) {
72+
res = res * a % m;
6273
}
63-
return res;
74+
a = a * a % m;
75+
// Dividing b by 2 is similar to right shift by 1 bit
76+
b >>= 1;
77+
}
78+
return res;
6479
}
65-
66-
/** Prime check in \f$O(\sqrt{m})\f$ time.
80+
/**
81+
* @brief Check if an integer is a prime number in \f$O(\sqrt{m})\f$ time.
82+
* @param m An intger to check for primality
83+
* @return true if the number is prime
84+
* @return false if the number is not prime
6785
*/
68-
bool isPrime(int64_t m) {
69-
if (m <= 1) {
70-
return false;
71-
} else {
72-
for (int64_t i = 2; i * i <= m; i++) {
73-
if (m % i == 0) {
74-
return false;
75-
}
76-
}
86+
bool isPrime(std::int64_t m) {
87+
if (m <= 1) {
88+
return false;
89+
}
90+
for (std::int64_t i = 2; i * i <= m; i++) {
91+
if (m % i == 0) {
92+
return false;
7793
}
78-
return true;
94+
}
95+
return true;
96+
}
97+
/**
98+
* @brief calculates the modular inverse.
99+
* @param a Integer value for the base
100+
* @param m Integer value for modulo
101+
* @return The result that is the modular inverse of a modulo m
102+
*/
103+
std::int64_t modular_inverse(std::int64_t a, std::int64_t m) {
104+
while (a < 0) {
105+
a += m;
106+
}
107+
108+
// Check for invalid cases
109+
if (!isPrime(m) || a == 0) {
110+
return -1; // Invalid input
111+
}
112+
113+
return binExpo(a, m - 2, m); // Fermat's Little Theorem
114+
}
115+
} // namespace modular_inverse_fermat
116+
} // namespace math
117+
118+
/**
119+
* @brief Self-test implementation
120+
* @return void
121+
*/
122+
static void test() {
123+
assert(math::modular_inverse_fermat::modular_inverse(0, 97) == -1);
124+
assert(math::modular_inverse_fermat::modular_inverse(15, -2) == -1);
125+
assert(math::modular_inverse_fermat::modular_inverse(3, 10) == -1);
126+
assert(math::modular_inverse_fermat::modular_inverse(3, 7) == 5);
127+
assert(math::modular_inverse_fermat::modular_inverse(1, 101) == 1);
128+
assert(math::modular_inverse_fermat::modular_inverse(-1337, 285179) == 165519);
129+
assert(math::modular_inverse_fermat::modular_inverse(123456789, 998244353) == 25170271);
130+
assert(math::modular_inverse_fermat::modular_inverse(-9876543210, 1000000007) == 784794281);
79131
}
80132

81133
/**
82-
* Main function
134+
* @brief Main function
135+
* @return 0 on exit
83136
*/
84137
int main() {
85-
int64_t a, m;
86-
// Take input of a and m.
87-
std::cout << "Computing ((a^(-1))%(m)) using Fermat's Little Theorem";
88-
std::cout << std::endl << std::endl;
89-
std::cout << "Give input 'a' and 'm' space separated : ";
90-
std::cin >> a >> m;
91-
if (isPrime(m)) {
92-
std::cout << "The modular inverse of a with mod m is (a^(m-2)) : ";
93-
std::cout << binExpo(a, m - 2, m) << std::endl;
94-
} else {
95-
std::cout << "m must be a prime number.";
96-
std::cout << std::endl;
97-
}
138+
test(); // run self-test implementation
139+
return 0;
98140
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* @file
3+
* @brief find the length of the Longest Increasing Subsequence (LIS)
4+
* using [Binary Search](https://en.wikipedia.org/wiki/Longest_increasing_subsequence)
5+
* @details
6+
* Given an integer array nums, return the length of the longest strictly
7+
* increasing subsequence.
8+
* The longest increasing subsequence is described as a subsequence of an array
9+
* where: All elements of the subsequence are in increasing order. This subsequence
10+
* itself is of the longest length possible.
11+
12+
* For solving this problem we have Three Approaches :-
13+
14+
* Approach 1 :- Using Brute Force
15+
* The first approach that came to your mind is the Brute Force approach where we
16+
* generate all subsequences and then manually filter the subsequences whose
17+
* elements come in increasing order and then return the longest such subsequence.
18+
* Time Complexity :- O(2^n)
19+
* It's time complexity is exponential. Therefore we will try some other
20+
* approaches.
21+
22+
* Approach 2 :- Using Dynamic Programming
23+
* To generate all subsequences we will use recursion and in the recursive logic we
24+
* will figure out a way to solve this problem. Recursive Logic to solve this
25+
* problem:-
26+
* 1. We only consider the element in the subsequence if the element is grater then
27+
* the last element present in the subsequence
28+
* 2. When we consider the element we will increase the length of subsequence by 1
29+
* Time Complexity: O(N*N)
30+
* Space Complexity: O(N*N) + O(N)
31+
32+
* This approach is better then the previous Brute Force approach so, we can
33+
* consider this approach.
34+
35+
* But when the Constraints for the problem is very larger then this approach fails
36+
37+
* Approach 3 :- Using Binary Search
38+
* Other approaches use additional space to create a new subsequence Array.
39+
* Instead, this solution uses the existing nums Array to build the subsequence
40+
* array. We can do this because the length of the subsequence array will never be
41+
* longer than the current index.
42+
43+
* Time complexity: O(n∗log(n))
44+
* Space complexity: O(1)
45+
46+
* This approach consider Most optimal Approach for solving this problem
47+
48+
* @author [Naman Jain](https://github.com/namanmodi65)
49+
*/
50+
51+
#include <cassert> /// for std::assert
52+
#include <iostream> /// for IO operations
53+
#include <vector> /// for std::vector
54+
#include <algorithm> /// for std::lower_bound
55+
#include <cstdint> /// for std::uint32_t
56+
57+
/**
58+
* @brief Function to find the length of the Longest Increasing Subsequence (LIS)
59+
* using Binary Search
60+
* @tparam T The type of the elements in the input vector
61+
* @param nums The input vector of elements of type T
62+
* @return The length of the longest increasing subsequence
63+
*/
64+
template <typename T>
65+
std::uint32_t longest_increasing_subsequence_using_binary_search(std::vector<T>& nums) {
66+
if (nums.empty()) return 0;
67+
68+
std::vector<T> ans;
69+
ans.push_back(nums[0]);
70+
for (std::size_t i = 1; i < nums.size(); i++) {
71+
if (nums[i] > ans.back()) {
72+
ans.push_back(nums[i]);
73+
} else {
74+
auto idx = std::lower_bound(ans.begin(), ans.end(), nums[i]) - ans.begin();
75+
ans[idx] = nums[i];
76+
}
77+
}
78+
return static_cast<std::uint32_t>(ans.size());
79+
}
80+
81+
/**
82+
* @brief Test cases for Longest Increasing Subsequence function
83+
* @returns void
84+
*/
85+
static void tests() {
86+
std::vector<int> arr = {10, 9, 2, 5, 3, 7, 101, 18};
87+
assert(longest_increasing_subsequence_using_binary_search(arr) == 4);
88+
89+
std::vector<int> arr2 = {0, 1, 0, 3, 2, 3};
90+
assert(longest_increasing_subsequence_using_binary_search(arr2) == 4);
91+
92+
std::vector<int> arr3 = {7, 7, 7, 7, 7, 7, 7};
93+
assert(longest_increasing_subsequence_using_binary_search(arr3) == 1);
94+
95+
std::vector<int> arr4 = {-10, -1, -5, 0, 5, 1, 2};
96+
assert(longest_increasing_subsequence_using_binary_search(arr4) == 5);
97+
98+
std::vector<double> arr5 = {3.5, 1.2, 2.8, 3.1, 4.0};
99+
assert(longest_increasing_subsequence_using_binary_search(arr5) == 4);
100+
101+
std::vector<char> arr6 = {'a', 'b', 'c', 'a', 'd'};
102+
assert(longest_increasing_subsequence_using_binary_search(arr6) == 4);
103+
104+
std::vector<int> arr7 = {};
105+
assert(longest_increasing_subsequence_using_binary_search(arr7) == 0);
106+
107+
std::cout << "All tests have successfully passed!\n";
108+
}
109+
110+
/**
111+
* @brief Main function to run tests
112+
* @returns 0 on exit
113+
*/
114+
int main() {
115+
tests(); // run self test implementation
116+
return 0;
117+
}

0 commit comments

Comments
 (0)