From 2ec60f920e172ed33613cdf9837070d7fcdc431d Mon Sep 17 00:00:00 2001 From: Anish Kumar Singh Date: Tue, 23 Mar 2021 00:40:04 +0530 Subject: [PATCH 1/4] New algorithms is added --- C++/Dynamic Programming/jungling.cpp | 77 ++++++++++++++++++++++++++++ C++/Math/pattern_find.cpp | 43 ++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 C++/Dynamic Programming/jungling.cpp create mode 100644 C++/Math/pattern_find.cpp diff --git a/C++/Dynamic Programming/jungling.cpp b/C++/Dynamic Programming/jungling.cpp new file mode 100644 index 0000000..69731e3 --- /dev/null +++ b/C++/Dynamic Programming/jungling.cpp @@ -0,0 +1,77 @@ +// C++ program to rotate an array by +// d elements +#include + +/*Fuction to get gcd of a and b*/ +int gcd(int a, int b) +{ + if (b == 0) + { + return a; + } + + else + { + return gcd(b, a % b); + } +} + +/*Function to left rotate arr[] of siz n by d*/ +void leftRotate(int arr[], int d, int n) +{ + int i,temp,k,j,g_c_d; + /* To handle if d >= n */ + d = d % n; + g_c_d = gcd(d, n); + for (i = 0; i < g_c_d; i++) + { + /* move i-th values of blocks */ + temp = arr[i]; + j = i; + + while (1) + { + int k = j + d; + if (k >= n) + { + k = k - n; + } + + if (k == i) + { + break; + } + + arr[j] = arr[k]; + j = k; + } + arr[j] = temp; + } +} + +// Function to print an array +void printArray(int arr[], int size) +{ + int i; + + for (i = 0; i < size; i++) + { + std::cout << arr[i] << " "; + } + std::cout << std::endl; +} + +/* Driver program to test above functions */ +int main() +{ + int arr[] = {1, 2, 3, 4, 5, 6, 7}; + int n = sizeof(arr) / sizeof(arr[0]); + std::cout << "Before rotating" << std::endl; + printArray(arr, n); + // Function calling + leftRotate(arr, 2, n); + std::cout << "after rotating by 2" << std::endl; + printArray(arr, n); + + return 0; +} \ No newline at end of file diff --git a/C++/Math/pattern_find.cpp b/C++/Math/pattern_find.cpp new file mode 100644 index 0000000..97569e3 --- /dev/null +++ b/C++/Math/pattern_find.cpp @@ -0,0 +1,43 @@ +//C++ code to find a pattern of string in another given string +#include +using namespace std; + +void search(string pat, string txt) +{ + int M = pat.size(); + int N = txt.size(); + int i = 0, j; + while (i <= N - M) + { + for (j = 0; j < M; j++) + { + if (txt[i + j] != pat[j]) + { + break; + } + } + if (j == M) + { + cout << " Pattern is found at index " << i << endl; + i = i + M; + } + else if (j == 0) + { + i = i + 1; + } + else + { + i = i + j; + } + } +} + +//Driver code +int main() +{ + string txt = "ABCEABCDABCEABCD"; + string pat = "ABCD"; + search(pat, txt); + + return 0; +} \ No newline at end of file From e03c03285bf1df9ba14a5209a69f9abc8b0a03a1 Mon Sep 17 00:00:00 2001 From: Anish Kumar Singh Date: Thu, 25 Mar 2021 01:00:24 +0530 Subject: [PATCH 2/4] C++ code to rotate a sorted array by d --- C++/Dynamic Programming/juggling.cpp | 86 ++++++++++++++++++++++++++++ C++/README.md | 2 +- 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 C++/Dynamic Programming/juggling.cpp diff --git a/C++/Dynamic Programming/juggling.cpp b/C++/Dynamic Programming/juggling.cpp new file mode 100644 index 0000000..3d11d0a --- /dev/null +++ b/C++/Dynamic Programming/juggling.cpp @@ -0,0 +1,86 @@ +// C++ program to rotate an sorted array by d elements +#include + +/*Fuction to get gcd of a and b*/ +int gcd(int a, int b) +{ + if (b == 0) + { + return a; + } + + else + { + return gcd(b, a % b); + } +} + +/*Function to left rotate arr[] of siz n by d*/ +void leftRotate(int arr[], int d, int n) +{ + int i,temp,k,j,g_c_d; + /* To handle if d >= n */ + d = d % n; + g_c_d = gcd(d, n); + for (i = 0; i < g_c_d; i++) + { + /* move i-th values of blocks */ + temp = arr[i]; + j = i; + + while (1) + { + int k = j + d; + if (k >= n) + { + k = k - n; + } + + if (k == i) + { + break; + } + + arr[j] = arr[k]; + j = k; + } + arr[j] = temp; + } +} + +// Function to print an array +void printArray(int arr[], int size) +{ + int i; + + for (i = 0; i < size; i++) + { + std::cout << arr[i] << " "; + } + std::cout << std::endl; +} + +/* Driver program to test above functions */ +int main() +{ + int arr[] = {1, 2, 3, 4, 5, 6, 7}; + int n = sizeof(arr) / sizeof(arr[0]); + std::cout << "Before rotating" << std::endl; + printArray(arr, n); + // Function calling + leftRotate(arr, 2, n); + std::cout << "After rotating by 2" << std::endl; + printArray(arr, n); + + return 0; +} +/* +Input: + arr[]={1, 2, 3, 4, 5, 6, 7} + d=2 +Output: + Before rotating + 1 2 3 4 5 6 7 + After rotating by 2 + 3 4 5 6 7 1 2 +*/ \ No newline at end of file diff --git a/C++/README.md b/C++/README.md index 959391c..1a639a8 100644 --- a/C++/README.md +++ b/C++/README.md @@ -27,7 +27,7 @@ - [Fibonacci DP](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Dynamic%20Programming/dp_fibonacci.cpp) - [Fractional Knapsack Problem](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Dynamic%20Programming/fractional_knapsack.c) - +-[Jungling problem](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Dynamic%20Programming/juggling.cpp) - [Longest Common Subsequence](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Dynamic%20Programming/lcs.cpp) - [Matrix Chain Multiplication](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Dynamic%20Programming/matrix_chain_multiplication.cpp) From 3c4ef333de3be71da206d6615182140e530a5419 Mon Sep 17 00:00:00 2001 From: Anish Kumar Singh Date: Fri, 26 Mar 2021 00:01:22 +0530 Subject: [PATCH 3/4] Updated --- C++/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/C++/README.md b/C++/README.md index 1a639a8..470c0b0 100644 --- a/C++/README.md +++ b/C++/README.md @@ -75,7 +75,7 @@ - [nCr_mod_prime](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/nCr_mod_prime.cpp) - [Pascal triangle](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/pascal_triangle.cpp) - +-[Pattern find](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/pattern_find.cpp) - [Sieve of eratosthenes](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/sieve_of_eratosthenes.cpp) From 99f687911103897208ae4917c70899c890d27e8e Mon Sep 17 00:00:00 2001 From: Anish Kumar Singh Date: Fri, 26 Mar 2021 00:08:43 +0530 Subject: [PATCH 4/4] made a little change in readme.md --- C++/README.md | 2 +- README.md | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/C++/README.md b/C++/README.md index 470c0b0..20476d5 100644 --- a/C++/README.md +++ b/C++/README.md @@ -75,7 +75,7 @@ - [nCr_mod_prime](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/nCr_mod_prime.cpp) - [Pascal triangle](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/pascal_triangle.cpp) --[Pattern find](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/pattern_find.cpp) +- [Pattern find](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/pattern_find.cpp) - [Sieve of eratosthenes](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/sieve_of_eratosthenes.cpp) diff --git a/README.md b/README.md index 72814b2..4cd936b 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,7 @@ You can also create an issue or [contact us](https://github.com/aniketsharma0041 - [Matrix exponentiation](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/matrix_exponentiation.cpp) - [nCr_mod_prime](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/nCr_mod_prime.cpp) - [Pascal triangle](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/pascal_triangle.cpp) +- [Pattern find](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/pattern_find.cpp) - [Sieve of eratosthenes](https://github.com/aniketsharma00411/algorithmsUse/blob/master/C%2B%2B/Math/sieve_of_eratosthenes.cpp)