Skip to content

New algorithms is added #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions C++/Dynamic Programming/juggling.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// C++ program to rotate an sorted array by d elements
#include <bits/stdc++.h>

/*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
*/
77 changes: 77 additions & 0 deletions C++/Dynamic Programming/jungling.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// C++ program to rotate an array by
// d elements
#include <bits/stdc++.h>

/*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;
}
43 changes: 43 additions & 0 deletions C++/Math/pattern_find.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//C++ code to find a pattern of string in another given string
#include <iostream>
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;
}
4 changes: 2 additions & 2 deletions C++/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

<a name="searching"></a>
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

<a name="searching"></a>
Expand Down