Repository navigation
Expand file tree
/
Copy pathRecursion.cpp
More file actions
561 lines (380 loc) · 9.26 KB
/
Copy pathRecursion.cpp
File metadata and controls
561 lines (380 loc) · 9.26 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
#include <iostream>
#include <limits.h>
#include <cmath>
#include <vector>
#include <string.h>
#include <algorithm>
#include <typeinfo>
using namespace std;
// Factorial using Recursion
int FactorialRec(int n){
// Base Case
if(n==0){
return 1;
}
if(n==1){
return 1;
}
// Recursion Call & Processing
int ans= n* FactorialRec(n-1);
return ans;
}
// Fibonacci Series using Recursion
int FibonacciSeriesRec(int n){
// Base Case
if(n==0){
return 0;
}
if(n==1){
return 1;
}
// Recursion Call & Processing
int ans= FibonacciSeriesRec(n-1) + FibonacciSeriesRec(n-2);
return ans;
}
// Sum of N numbers using Recursion
int SumofNRec(int n){
// Base Case
if(n==0){
return 0;
}
// Processing and Recursion Call
int sum= n + SumofNRec(n-1);
return sum;
}
// Printing Array using Recursion
void PrintArrayRec(int *arr, int size, int index){
// Base Case
if(index>=size){
return;
}
// Processing
cout<<arr[index]<<" ";
// Recursion Call
PrintArrayRec(arr, size, index+1);
return;
}
// Linear Search using Recursion
bool LinearSearchRec(int *arr, int size, int index, int target){
// Base Case
if(index>=size){
return false;
}
// Processing
if(arr[index]==target){
return true;
}
// Recursion Call
bool ans= LinearSearchRec(arr, size, index+1, target);
return ans;
}
// Linear Search returning index using Recursion
int LinearSearchRecIn(int *arr, int size, int index, int target, int &ans){
// Base Case
if(index>=size){
return ans;
}
// Processing
if(arr[index]==target){
ans=index;
}
// Recursion Call
LinearSearchRecIn(arr, size, index+1, target, ans);
return ans;
}
// Min Number in an Array using Recursion
int MinNumRec(int *arr, int size, int index, int &mini){
// Base Case
if(index>=size){
return mini;
}
// Processing
mini= min(mini, arr[index]);
// Recursion Call
MinNumRec(arr, size, index+1, mini);
return mini;
}
// Even Number Pushing
void EvenNumPushing(int *arr, int size, int index, vector<int>& even){
// Base case
if(index>=size){
return;
}
// Processing
if(!(arr[index] & 1)){
even.push_back(arr[index]);
}
// Recursion Call
EvenNumPushing(arr, size, index+1, even);
return;
}
// Array Number Doubling
void NumberDoubling(int *arr, int size, int index){
// Base Case
if(index>=size){
return;
}
// Processing
arr[index]= 2*arr[index];
// Recursion Call
NumberDoubling(arr, size, index+1);
return;
}
// Max Number in an Array using Recursion
int MaxNumRec(int *arr, int size, int index, int &maxi){
// Base Case
if(index>=size){
return maxi;
}
// Processing
maxi= max(maxi, arr[index]);
// Recursion Call
MaxNumRec(arr, size, index+1, maxi);
return maxi;
}
// Print index of all targets in Array using Recursion
void PrtInTarget(int *arr, int size, int index, int target){
// Base Case
if(index>=size){
return;
}
// Processing
if(arr[index]== target){
cout<<index<<" ";
}
// Recursion Call
PrtInTarget(arr, size, index+1, target);
return;
}
// Storing digits of integer in a vector using Recursion
void DigitsInVector(int n, vector<int> &arr){
// Base Case
if(n==0){
return;
}
// Processing
int temp= n%10;
n= n/10;
// Recursion Call
DigitsInVector(n, arr);
// Processing
arr.push_back(temp);
return;
}
// Creating an interger using digits in Vector using Recursion
void IntegerThrghVector(vector<int> &arr, int index, int &ans, int multiplier){
// Base Case
if(index<0){
return;
}
// Processing
ans= ans+ arr[index]*multiplier;
// Recursion Call
IntegerThrghVector(arr, index-1, ans, multiplier*10);
return;
}
// Checking sorting of an array
bool CheckSortedArray(int *arr, int size, int index, bool &ans){
// Base Case
if(index>=size){
ans= true;
return ans;
}
// Processing and Recursion Call
else if(arr[index]>arr[index -1] || arr[index]==arr[index -1]){
CheckSortedArray(arr, size, index+1, ans);
}
return ans;
}
// Binary Search using Recursion
bool BinarySearchRec(int *arr, int target, int start, int end, bool &ans){
// Base Case
if(start>end){
return ans;
}
// Processing
int mid= start + (end-start)/2;
if(arr[mid]==target){
ans= true;
return ans;
}
else if(arr[mid]<target){
// Recursion Call
BinarySearchRec(arr, target, mid+1, end, ans);
}
else if(arr[mid]>target){
// Recursion Call
BinarySearchRec(arr, target, start, mid-1, ans);
}
return ans;
}
// Binary Search returning index using Recursion
int BinarySearchRecIn(int *arr, int target, int start, int end, int &ans){
// Base Case
if(start>end){
return ans;
}
// Processing
int mid= start + (end-start)/2;
if(arr[mid]==target){
ans= mid;
return ans;
}
else if(arr[mid]<target){
// Recursion Call
BinarySearchRecIn(arr, target, mid+1, end, ans);
}
else if(arr[mid]>target){
// Recursion Call
BinarySearchRecIn(arr, target, start, mid-1, ans);
}
return ans;
}
// Sub Sequences of a string, Include first
void SubSeqM1(string input, string output, int index){
// Base Case
if(index>=input.length()){
cout<<"-> "<<output<<endl;
return;
}
// Processing (Include)
output.push_back(input[index]);
// Recursion Call
SubSeqM1(input, output, index+1);
// Processing (Exclude)
output.pop_back();
// Recursion Call
SubSeqM1(input, output, index+1);
return;
}
// Sub Sequences of a string, Exclude first
void SubSeqM2(string input, string output, int index){
// Base Case
if(index>=input.length()){
cout<<"-> "<<output<<endl;
return;
}
// Processing (Exclude) and Recursion Call
SubSeqM2(input, output, index+1);
// Processing (Include)
output.push_back(input[index]);
// Recursion Call
SubSeqM2(input, output, index+1);
return;
}
// Maximize The Cut Segments
int MaximizeCutSeg(int n, int x, int y, int z){
// Base Case
if(n==0){
return 0;
}
if(n<0){
return INT_MIN;
}
// Recursion
int opt1= 1+ MaximizeCutSeg(n-x, x, y, z);
int opt2= 1+ MaximizeCutSeg(n-y, x, y, z);
int opt3= 1+ MaximizeCutSeg(n-z, x, y, z);
// Processing
int ans= max(opt1, max(opt2, opt3));
return ans;
}
// Coin Change
int CoinChange(vector<int> &coins, int amount){
// Base Case
if(amount==0){
return 0;
}
// Recursion call and Processing
int ans= INT_MAX;
int mini= INT_MAX;
for(int coin: coins){
if(coin<=amount){
int RecAns= CoinChange(coins, amount-coin);
if(RecAns != INT_MAX){
ans= 1+ RecAns;
}
}
}
mini= min(mini, ans);
return mini;
}
// House Robber
int Robber(vector<int> &nums, int index){
// Base Case
if(index>= nums.size()){
return 0;
}
// Recursion Call and Processing
int Opt1= nums[index] + Robber(nums, index+2);
int Opt2= Robber(nums, index+1);
int ans= max(Opt1, Opt2);
return ans;
}
int main(){
// int factorial= FactorialRec(5);
// cout<<factorial<<endl;
// int fibonacci= FibonacciSeriesRec(8);
// cout<<fibonacci<<endl;
// int sum= SumofNRec(5);
// cout<<sum<<endl;
// int arr[10]= {11,29,38,4,55,69,70,81,92,10};
// int size=10;
// int index=0;
// PrintArrayRec(arr, size, index);
// int target= 6;
// int ans=-1;
// int mini= INT_MAX;
// MinNumRec(arr, size, index, mini);
// cout<<mini<<endl;
// int maxi= INT_MIN;
// MaxNumRec(arr, size, index, maxi);
// cout<<maxi<<endl;
// bool Lsearch= LinearSearchRec(arr, size, index+1, target);
// cout<<Lsearch<<endl;
// LinearSearchRecIn(arr, size, index, target, ans);
// cout<<ans<<endl;
// vector<int> even;
// EvenNumPushing(arr, size, index+1, even);
// NumberDoubling(arr, size, index);
// for(int nums: arr){
// cout<<nums<<" ";
// }
// int arr[10]= {11,25,38,25,55,69,25,81,92,10};
// int size=10;
// int index=0;
// int target= 25;
// PrtInTarget(arr, size, index, target);
// int n=4219;
// vector<int> arr;
// DigitsInVector(n, arr);
// for(int nums: arr){
// cout<<nums<<" ";
// }
// int index=arr.size()-1;
// int ans=0;
// int multiplier=1;
// IntegerThrghVector(arr, index, ans, multiplier);
// cout<<ans<<endl;
// int arr[5]= {10,20,20,40,50};
// int size=5;
// int index=1;
// bool ans= CheckSortedArray(arr, size, index);
// cout<<ans<<endl;
// int arr[9]= {10,20,20,40,50,60,70,80,90};
// int size=9;
// int target= 60;
// int start=0;
// int end=size -1;
// bool ans= false;
// BinarySearchRec(arr, target, start, end, ans);
// int ans= -1;
// BinarySearchRecIn(arr, target, start, end, ans);
// cout<<ans<<endl;
string input= "abc";
string output= "";
int index=0;
SubSeqM1(input, output, index);
// SubSeqM2(input, output, index);
return 0;
}