-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecorsin-medium-level.cpp
More file actions
537 lines (432 loc) Β· 12.1 KB
/
Copy pathrecorsin-medium-level.cpp
File metadata and controls
537 lines (432 loc) Β· 12.1 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
// πΈ 2. Parameterized vs Functional Recursion
// β
(A) Parameterized Recursion
// π Mathematical Idea
// Carry answer along:
// S(n,sum)=S(nβ1,sum+n)
// π Answer stored in parameter
// Each call updates the sum and waits for the smaller problem to solve.
// S(n,sum)=S(nβ1,sum+n)
// Base case:
// S(0,sum)=sum
// Each call updates the sum and waits for the smaller problem to solve.
// S(n,sum)=S(nβ1,sum+n)
// #include<bits/stdc++.h>
// using namespace std;
// void sum(int n,int s){
// if(n==0){
// cout<<s<<endl;
// return;
// }
// sum(n-1,s+n);
// }
// int main(){
// int n;
// cin>>n;
// sum(n,0);
// return 0;
// }
// β
(B) Functional Recursion
// π Mathematical Idea
// S(n)=n+S(nβ1)
// π Return answer
// Each call waits for the answer of the smaller problem.
// S(n)=n+S(nβ1)
// Base case:
// S(0)=0
// Each call waits for the sum of the first n-1 numbers.
// S(n)=n+S(nβ1)
// #include<bits/stdc++.h>
// using namespace std;
// int sum(int n){
// if(n==0) return 0;
// return n+sum(n-1);
// }
// int main(){
// int n;
// cin>>n;
// cout<<sum(n)<<endl;
// return 0;
// }
// πΈ 3. Backtracking Basics
// π Core Idea
// π βDo β Explore β Undoβ
// Used to generate all possibilities
// π Pick / Not Pick Pattern
// At each index:
// Pick element
// Do not pick element
// π Explore both possibilities
// π Recursive Relation
// f(i)=f(i+1)+f(i+1)
// π Explore both possibilities at each index
// Base case:
// f(n)=1
// When we reach the end of the array, we have found a valid combination, so we return 1.
// πΉ 1. Subsequence Pattern β
// π Mathematical Idea
// For each element:
// choices={pick,not pick}
// Total subsequences:
//2^n;
// #include<bits/stdc++.h>
// using namespace std;
// void subsequence(int i,vector<int>&arr,vector<int>&ds){
// if(i==arr.size()){
// for(auto it:ds){
// cout<<it<<" ";
// }
// cout<<endl;
// return;
// }
// // Pick the element
// ds.push_back(arr[i]);
// subsequence(i+1,arr,ds);
// // Not pick the element
// ds.pop_back();
// subsequence(i+1,arr,ds);
// }
// int main(){
// int n;
// cin>>n;
// vector<int>arr(n);
// for(int i=0;i<n;i++){
// cin>>arr[i];
// }
// vector<int>ds;
// subsequence(0,arr,ds);
// return 0;
// }
// πΉ Subsequence Sum = K
// mathmatical Idea
// For each element:
// choices={pick,not pick}
// Total subsequences:
//2^n;
// Base case:
// When we reach the end of the array, we check if the sum of the current subsequence equals K. If it does, we return 1 to count this valid subsequence; otherwise, we return 0.
// f(i,sum)=f(i+1,sum+arr[i])+f(i+1,sum)
// π Explore both possibilities at each index and keep track of the sum
// Base case:
// f(n,sum)=1 if sum==K else 0
// When we reach the end of the array, we check if the sum of the current subsequence equals K. If it does, we return 1 to count this valid subsequence; otherwise, we return 0.
//
// #include<bits/stdc++.h>
// using namespace std;
// int subsequenceSumK(int i,vector<int>&arr,int sum,int k){
// if(i==arr.size()){
// return sum==k?1:0;
// }
// // Pick the element
// sum+=arr[i];
// int pick=subsequenceSumK(i+1,arr,sum,k);
// // Not pick the element
// sum-=arr[i];
// int notPick=subsequenceSumK(i+1,arr,sum,k);
// return pick+notPick;
// }
// int main(){
// int n;
// cin>>n;
// vector<int>arr(n);
// for(int i=0;i<n;i++){
// cin>>arr[i];
// }
// int k;
// cin>>k;
// cout<<subsequenceSumK(0,arr,0,k)<<endl;
// return 0;
// }
//πΉ Count Subsequences = K
// mathmatical Idea
// For each element:
// choices={pick,not pick}
// Total subsequences:
//2^n;
// Base case:
// When we reach the end of the array, we check if the sum of the current subsequence equals K. If it does, we return 1 to count this valid subsequence; otherwise, we return 0.
// f(i,sum)=f(i+1,sum+arr[i])+f(i+1,sum)
// π Explore both possibilities at each index and keep track of the sum
// Base case:
// f(n,sum)=1 if sum==K else 0
// #include<bits/stdc++.h>
// using namespace std;
// int countSubsequence(int i,vector<int>&arr,int sum,int k){
// if(i==arr.size()){
// return (sum==k);
// }
// // Pick the element
// int pick=countSubsequence(i+1,arr,sum+arr[i],k);
// // Not pick the element
// int notPick=countSubsequence(i+1,arr,sum,k);
// return pick+notPick;
// }
// int main(){
// int n;
// cin>>n;
// vector<int>arr(n);
// for(int i=0;i<n;i++){
// cin>>arr[i];
// }
// int k;
// cin>>k;
// cout<<countSubsequence(0,arr,0,k)<<endl;
// return 0;
// }
// πΉ 2. Subset / Power Set
// π Same as subsequence
// Total subsets:
//2^n;
// #include<bits/stdc++.h>
// using namespace std;
// vector<vector<int>>ans;
// void subset(int i,vector<int>&arr,vector<int>&ds){
// if(i==arr.size()){
// ans.push_back(ds);
// return;
// }
// // Pick the element
// ds.push_back(arr[i]);
// subset(i+1,arr,ds);
// // Not pick the element
// ds.pop_back();
// subset(i+1,arr,ds);
// }
// int main(){
// int n;
// cin>>n;
// vector<int>arr(n);
// for(int i=0;i<n;i++){
// cin>>arr[i];
// }
// vector<int>ds;
// subset(0,arr,ds);
// for(auto it:ans){
// for(auto i:it){
// cout<<i<<" ";
// }
// cout<<endl;
// }
// return 0;
// }
// πΉ 3. Permutations
// π Idea
// Fix one element, permute rest
// Total permutations:
// n!
// Base case:
// When we have fixed all elements, we have found a valid permutation, so we return 1 to count this valid permutation; otherwise, we return 0.
// f(i)=f(i+1)+f(i+1)+β¦+f(i+n-1)
// π Explore all possibilities by swapping elements at each index
// Base case:
// f(n)=1
// When we have fixed all elements, we have found a valid permutation, so we return 1 to count this valid permutation; otherwise, we return 0.
// #include<bits/stdc++.h>
// using namespace std;
// int cnt=0;
// void permute(int i,vector<int>&arr){
// if(i==arr.size()){
// for(int x:arr){
// cout<<x<<" ";
// }
// cnt++;
// cout<<endl;
// }
// for(int j=i;j<arr.size();j++){
// swap(arr[i],arr[j]);
// permute(i+1,arr);
// swap(arr[i],arr[j]);// Backtrack to restore original array
// }
// }
// int main(){
// int n;
// cin>>n;
// vector<int>arr(n);
// for(int i=0;i<n;i++){
// cin>>arr[i];
// }
// permute(0,arr);
// cout<<cnt<<endl;
// return 0;
// }
// πΉ 4. Combinations
// π» Combination Sum
// π Idea
// Fix one element, combine with rest
// Total combinations:
// 2^n
// Base case:
// When we have fixed all elements, we have found a valid combination, so we return 1 to count this valid combination; otherwise, we return 0.
// f(i)=f(i+1)+f(i+1)+β¦+f(i+n-1)
// π Explore all possibilities by including elements at each index
// Base case:
// f(n)=1
// When we have fixed all elements, we have found a valid combination, so we return 1 to count this valid combination; otherwise, we return 0.
// #include<bits/stdc++.h>
// using namespace std;
// int cnt=0;
// void combSum(int i,vector<int>&arr,int target,vector<int>&ds){
// if(i==arr.size()){
// if(target==0){
// for(int x:ds){
// cout<<x<<" ";
// }
// cnt++;
// cout<<endl;
// }
// return;
// }
// // Pick the element
// if(arr[i]<=target){
// ds.push_back(arr[i]);
// combSum(i,arr,target-arr[i],ds);
// ds.pop_back();
// }
// // Not pick the element
// combSum(i+1,arr,target,ds);
// }
// int main(){
// int n;
// cin>>n;
// vector<int>arr(n);
// for(int i=0;i<n;i++){
// cin>>arr[i];
// }
// int target;
// cin>>target;
// vector<int>ds;
// combSum(0,arr,target,ds);
// cout<<cnt<<endl;
// return 0;
// }
// πΉ 5. String Recursion
// πΉ Reverse String
//mathmatical Idea
// For each character:
// choices={pick,not pick}
// Total subsequences:
//2^n;
// Base case:
// When we reach the end of the string, we have found a valid reversed string, so
// we return 1 to count this valid reversed string; otherwise, we return 0.
// f(i)=f(i+1)+f(i+1)
// π Explore both possibilities at each index and build the reversed string
// Base case:
// f(n)=1
// When we reach the end of the string, we have found a valid reversed string, so
// we return 1 to count this valid reversed string; otherwise, we return 0.
// #include<bits/stdc++.h>
// using namespace std;
// void reverseString(string &s,int i,int j){
// if(i>=j)return;
// swap(s[i],s[j]);
// reverseString(s,i+1,j-1);
// }
// int main(){
// string s;
// cin>>s;
// reverseString(s,0,s.size()-1);
// cout<<s<<endl;
// return 0;
// }
// πΉ Palindrome Check
// mathmatical Idea
// For each character:
// choices={pick,not pick}
// Total subsequences:
//2^n;
// Base case:
// When we reach the end of the string, we have found a valid palindrome, so
// we return 1 to count this valid palindrome; otherwise, we return 0.
// f(i)=f(i+1)+f(i+1)
// π Explore both possibilities at each index and check for palindrome
// Base case:
// f(n)=1
// When we reach the end of the string, we have found a valid palindrome, so
// we return 1 to count this valid palindrome; otherwise, we return 0.
// #include<bits/stdc++.h>
// using namespace std;
// bool isPalindrome(string &s,int i){
// int n=s.size();
// if(i>=n/2) return true;
// if(s[i]!=s[n-1-i]) return false;
// return isPalindrome(s,i+1);
// }
// int main(){
// string s;
// cin>>s;
// if(isPalindrome(s,0)){
// cout<<"Palindrome"<<endl;
// }else{
// cout<<"Not a Palindrome"<<endl;
// }
//}
//πΉ Find Max
// mathmatical Idea
//max(arr[0],max(arr[1],max(arr[2],β¦max(arr[n-2],arr[n-1])β¦)))
// Base case:
// When we have compared all elements, we have found the maximum element, so we return that maximum element; otherwise, we return 0.
// f(i)=max(arr[i],f(i+1))
// π Explore all possibilities by comparing elements at each index
// Base case:
// f(n)=arr[n-1]
// When we have compared all elements, we have found the maximum element, so we return that maximum element; otherwise, we return 0.
// #include<bits/stdc++.h>
// using namespace std;
// int findMax(int i,vector<int>&arr) {
// if(i==arr.size()-1){
// return arr[i];
// }
// int maxRest=findMax(i+1,arr);
// return max(arr[i],maxRest);
// }
// int main(){
// int n;
// cin>>n;
// vector<int>arr(n);
// for (int i = 0; i < n; i++) {
// cin >> arr[i];
// }
// cout << findMax(0, arr) << endl;
// return 0;
// }
// πΉ 7. Binary Search (Recursion)
// π Mathematical Idea
// Divide range:
// mid= low+(high-low)/2
// Base case:
// When we have found the target element, we return its index; otherwise, if the target element is not found in the array, we return -1.
// f(low,high)=f(low,mid-1) if target<mid
// f(low,high)=f(mid+1,high) if target>mid
// π Explore the half of the array that may contain the target element
// Base case:
// f(low,high)=mid if arr[mid]==target
// When we have found the target element, we return its index; otherwise, if the target element is not found in the array, we return -1.
// #include<bits/stdc++.h>
// using namespace std;
// int binarySearch(int low,int high,vector<int>&arr,int target){
// if(low>high) return -1;
// int mid=low+(high-low)/2;
// if(arr[mid]==target) return mid;
// if(target<arr[mid]){
// return binarySearch(low,mid-1,arr,target);
// }else{
// return binarySearch(mid+1,high,arr,target);
// }
// }
// int main(){
// int n;
// cin>>n;
// vector<int>arr(n);
// for(int i=0;i<n;i++){
// cin>>arr[i];
// }
// int target;
// cin>>target;
// int result=binarySearch(0,n-1,arr,target);
// if(result!=-1){
// cout<<"Element found at index: "<<result<<endl;
// }
// else{
// cout<<"Element not found in the array."<<endl;
// }
// }