-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbegginersrecursion.cpp
224 lines (221 loc) · 4.12 KB
/
begginersrecursion.cpp
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int count = 0;
// void print()
// {
// // We are going to use these varible for testing purpose
// if (count == 5)
// {
// return;
// }
// else
// {
// count++;
// cout << count << endl;
// print();
// }
// }
int cntname = 0;
void printname()
{
if (cntname == 5)
{
return;
}
else
{
cout << "THE ATHARV" << endl;
cntname++;
printname();
}
}
void printn(int i, int n)
{
if (i > n)
{
return;
}z
else
{
cout << "Atharv powalkr these is good programmer and has ability to do anything in the life" << endl;
printn(i + 1, n);
}
}
void printoneton(int i, int n)
{
if (i < n)
{
return;
}
else
{
cout << i << endl;
printoneton(i - 1, n);
}
}
void backtrackoneton(int i, int n)
{
if (i < n)
{
return;
}
else
{
backtrackoneton(i - 1, n);
// backtrackoneton(3,1)
// backtrackoneton(2,1)
// backtrackoneton(1,1)
cout << i << endl;
}
}
void backtrack(int i, int n)
{
if (i > n)
{
return;
}
else
{
backtrack(i + 1, n);
cout << i << endl;
}
}
int sumrec(int n, int sum)
{
if (n < 1)
{
return sum;
}
else
{
sumrec(n - 1, sum + n);
// sum(3,0)
// sum(2,3)
// sum(1,5)
// sum(0,6)
}
}
// These is paremeterized recurions
// There are changing the parameters so these is the parameterized recursion
void sumtry(int n, int sum)
{
if (n < 1)
{
cout << sum << endl;
}
else
{
sumtry(n - 1, sum + n);
}
}
int sumoo(int n)
{
if (n == 0)
{
return n;
}
else
{
return n + sumoo(n - 1);
}
}
int fact(int n)
{
if (n == 1)
{
return n;
}
else
{
return n * fact(n - 1);
}
}
// Using the 2 pointer
void revusingrec(int left, int right, int arr[])
{
if (left >= right)
{
return;
}
else
{
swap(arr[left], arr[right]);
revusingrec(left + 1, right - 1, arr);
}
}
// void revarrussingptr(int position, int arr[], int n)
// {
// if (position > n / 2)
// {
// return;
// }
// else
// {
// // using the single pointer if the pointer crosses the n/2 then we have to stop
// swap(arr[position], arr[n - position - 1]);
// revarrussingptr(position + 1);
// }
// }
bool checkpalin(int i, string &s)
{
if (i >= s.size() / 2)
{
return true;
}
if (s[i] != s[s.size() - i - 1])
{
return false;
}
return checkpalin(i + 1, s);
}
int multiplerec(int n)
{
if (n <= 1)
{
return n;
}
int l = multiplerec(n - 1);
int r = multiplerec(n - 2);
return l + r;
}
int main()
{
cout << "We are going to learn the recursion in these tut" << endl;
// print();
// printname();
// printn(1, 5);
// printoneton(1, 10);
// printoneto100(1, 100);
// printoneton(100, 1);
// backtrackoneton(4, 1);
// backtrack(1, 4);
// int sum = sumrec(3, 0);
// cout << sum << endl;
// sumtry(20, 0);
// int t = sumoo(20);
// cout << t << endl;
// int f = fact(5);
// cout << f << endl;
// int arr[5] = {12, 54, 67, 87, 34};
// int n = 5;
// revusingrec(0, n - 1, arr);
// cout << "Aarray after swapping" << endl;
// for (int i = 0; i < n; i++)
// {
// cout << arr[i] << endl;
// }
// cout << "Successfully" << endl;
// These is functional recursive function
string store = "madam";
if (checkpalin(0, store))
{
cout << "THESE IS " << endl;
}
else
{
cout << "THESE IS NOT" << endl;
}
int result = multiplerec(9);
cout << result << endl;
}