-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicRecursion.cpp
More file actions
144 lines (128 loc) · 2.64 KB
/
Copy pathBasicRecursion.cpp
File metadata and controls
144 lines (128 loc) · 2.64 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
#include <bits/stdc++.h>
using namespace std;
int cnt = 1;
void print()
{
if (cnt == 3)
return;
cout << cnt << "\n";
cnt++;
print();
}
void printName(int i, int n)
{
// Time Complexity=O(n)
// Space Complexity or Stack Space=O(n)
if (i > n)
return;
cout << "Ujjawal" << "\n";
printName(i + 1, n);
}
void print1ToN(int i, int n)
{
if (i > n)
return;
cout << i << "\n";
print1ToN(i + 1, n);
}
void printNTo1(int i, int n)
{
if (i == 0)
return;
cout << i << "\n";
printNTo1(i - 1, n);
}
// using backtracking
void printNTo1usingB(int i, int n)
{
if (i > n)
return;
printNTo1usingB(i + 1, n);
cout << i << "\n";
}
// using paramterised way
void sumOfNterms(int i, int sum)
{
if(i<1)
{
cout<<sum<<"\n";
return;
}
sumOfNterms(i-1,sum+i);
}
// using functional way
int sumOfNtermsUsingf(int n)
{
if(n==0) return 0;
return n+sumOfNtermsUsingf(n-1);
}
int fact(int n)
{
if(n==1) return 1;
return n*fact(n-1);
}
// Two pinter approach
void revArray(int l,int r,int arr[])
{
if(l>=r) return;
// int temp=arr[l];
// arr[l]=arr[r];
// arr[r]=temp;
// or
swap(arr[l],arr[r]);
revArray(l+1,r-1,arr);
}
// using single variable
void revArrayUsingSv(int i,int arr[],int n)
{
if(i>=n/2) return; // or i>=n-i-1 should also work
swap(arr[i],arr[n-i-1]);
revArrayUsingSv(i+1,arr,n);
}
bool checkPalindrome(int i,string str,int n)
{
if(i>=n-i-1) return true;
if(str[i]!=str[n-i-1]) return false;
return checkPalindrome(i+1,str,n);
}
// Time Complexity - O(2^n) ; using functional way with two recc calls
int fibonacci(int n)
{
if(n<=1) return n;
return fibonacci(n-1)+fibonacci(n-2);
}
// Time Complexity - O(n) ; using parametrized way with single recc call
int fib(int n,int a,int b)
{
if(n==1) return b;
if(n==0) return a;
return fib(n-1,b,a+b);
}
void printArray(int arr[],int n)
{
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
cout<<"\n";
}
int main()
{
// printName(1,5);
// print1ToN(1,5);
// printNTo1(5,5);
// printNTo1usingB(1, 5);
// sumOfNterms(3,0);
// cout<<sumOfNtermsUsingf(3)<<"\n";
// cout<<fact(4)<<"\n";
// int arr[]={1,2,3,4};
// int n=sizeof(arr)/sizeof(arr[0]); // or size(arr);
// printArray(arr,n);
// revArrayUsingSv(0,arr,n);
// printArray(arr,n);
// string str="MadaM";
// int n=str.size(); // .size() can be used for all stl containers but not for classic c style arrays
// cout<<checkPalindrome(0,str,n);
cout<<fibonacci(4)<<"\n";
// cout<<fib(0,0,1)<<"\n";
}