-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecursion Questions
193 lines (141 loc) · 3.15 KB
/
Recursion Questions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//Factorial
#include <iostream>
using namespace std;
int factorial(int n) {
//base case
if(n == 1)
return 1;
int chotiProblemAns = factorial(n-1);
int badiProblemAns = n * chotiProblemAns;
return badiProblemAns;
}
int main() {
int n;
cout << "Enter the value of n " << endl;
cin >> n;
int ans = factorial(n);
cout << "Answer is: " << ans << endl;
return 0;
}
//Print Counting
#include <iostream>
using namespace std;
void printCounting(int n) {
//base case
if(n == 0) {
return;
}
//processing
cout << n << " ";
//re4ciursive relation
printCounting(n-1);
}
int main() {
int n;
cout << "Enter the value of n " << endl;
cin >> n;
printCounting(n);
cout << endl;
return 0;
}
//Nth Term
#include <iostream>
using namespace std;
int fib(int n) {
//base case
if(n == 1) {
//first term
return 0;
}
if(n == 2) {
//second term
return 1;
}
//RR -> f(n) = f(n-1) + f(n-2)
int ans = fib(n-1) + fib(n-2);
return ans;
}
int main() {
int n;
cout << "Enter the term you want to see" << endl;
cin >> n;
int ans = fib(n);
cout << n <<"th term is: " << ans << endl;
//Finding Key
#include <iostream>
#include<vector>
using namespace std;
void checkKey(string& str,int i, int& n, char& key, int& count) {
//base case
if(i >= n) {
//key not found
return ;
}
//1 case solve krdo
if(str[i] == key) {
///store in vector
//ans.push_back(i);
count++;
}
//baaki recursion sambhal lega
checkKey(str, i+1, n, key, count);
}
int main() {
string str = "lovebabbar";
int n = str.length();
char key = 'b';
int i = 0;
//vector<int> ans;
int count = 0;
checkKey(str,i, n, key, count);
cout << count << endl;
cout << "printing ans" << endl;
for(auto val: ans) {
cout << val << " " ;
}
cout << endl;
return 0;
}
return 0;
}
//Find Min and Max Function
#include <iostream>
#include<limits.h>
using namespace std;
void findMax(int arr[], int n, int i, int& maxi) {
//base case
if(i >= n) {
//array agar khtam hogya, poora traverse hogya
//toh wapas aajao
return;
}
//1 case solve krna h
//current element ko cjheck karo for max
if(arr[i] > maxi) {
maxi = arr[i];
}
//baaki recursion sambhal lega
findMax(arr, n, i+1, maxi);
}
void findMin(int arr[], int n, int i, int& mini ){
//base case
if( i >= n) {
return;
}
//1 case solve krna padega
mini = min(mini, arr[i]);
//baaki recursion sambhal lega
findMin(arr, n, i+1, mini);
}
int main() {
int arr[] = {10,30,21,44,32,6,19,66};
int n = 8;
int maxi = INT_MIN;
int mini = INT_MAX;
int i = 0;
findMax(arr, n,i, maxi);
findMin(arr, n, i, mini);
cout << "maximum number is: "<< maxi << endl;
cout << "minimum number is: "<< mini << endl;
return 0;
}