Skip to content

Commit a2454bd

Browse files
committed
23March
1 parent 53407f8 commit a2454bd

3 files changed

Lines changed: 100 additions & 0 deletions

File tree

Recursion/GFG_JosephusProblem.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
https://practice.geeksforgeeks.org/problems/josephus-problem/1
3+
Josephus problem
4+
*/
5+
// { Driver Code Starts
6+
#include <bits/stdc++.h>
7+
using namespace std;
8+
9+
10+
11+
// } Driver Code Ends
12+
/*You are required to complete this method */
13+
14+
class Solution
15+
{
16+
public:
17+
int josephus(int n, int k)
18+
{
19+
if(n==1) return 1;
20+
return (josephus(n-1,k) + k-1)%n+1;
21+
}
22+
};
23+
24+
25+
26+
// { Driver Code Starts.
27+
28+
int main() {
29+
30+
int t;
31+
cin>>t;//testcases
32+
while(t--)
33+
{
34+
int n,k;
35+
cin>>n>>k;//taking input n and k
36+
37+
//calling josephus() function
38+
Solution ob;
39+
cout<<ob.josephus(n,k)<<endl;
40+
}
41+
return 0;
42+
} // } Driver Code Ends

Recursion/GFG_TowerOfHanoi.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
https://practice.geeksforgeeks.org/problems/tower-of-hanoi-1587115621/1#
3+
Tower Of Hanoi
4+
*/
5+
// { Driver Code Starts
6+
#include <bits/stdc++.h>
7+
8+
using namespace std;
9+
10+
11+
// } Driver Code Ends
12+
class Solution{
13+
public:
14+
// You need to complete this function
15+
16+
long long steps=0;
17+
// avoid space at the starting of the string in "move disk....."
18+
long long toh(int N, int from, int to, int aux) {
19+
steps++;
20+
if(N==1)
21+
{
22+
cout<<"move disk "<<N<<" from rod "<<from<<" to rod "<<to<<endl;
23+
return 1;
24+
}
25+
26+
27+
toh(N-1, from, aux, to);
28+
cout<<"move disk "<<N<<" from rod "<<from<<" to rod "<<to<<endl;
29+
toh(N-1, aux, to, from);
30+
31+
32+
return steps;
33+
// return pow(2,N)-1;
34+
}
35+
36+
};
37+
38+
// { Driver Code Starts.
39+
40+
int main() {
41+
42+
int T;
43+
cin >> T;//testcases
44+
while (T--) {
45+
46+
int N;
47+
cin >> N;//taking input N
48+
49+
//calling toh() function
50+
Solution ob;
51+
52+
cout << ob.toh(N, 1, 3, 2) << endl;
53+
}
54+
return 0;
55+
}
56+
57+
58+
//Position this line where user code will be pasted. // } Driver Code Ends

0 commit comments

Comments
 (0)