-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursionBasics.java
More file actions
221 lines (163 loc) · 4.86 KB
/
RecursionBasics.java
File metadata and controls
221 lines (163 loc) · 4.86 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
public class RecursionBasics {
// Print form n to 1
public static void printDec(int n){
if(n == 1){
System.out.println(n);
return;
}
System.out.print(n+" ");
printDec(n-1);
}
// Print from 1 to n
public static void printInc(int n) {
if(n == 1) {
System.out.print(n+" ");
return;
}
printInc(n-1);
System.out.print(n+" ");
}
// Factorial of a Number
public static int fact(int n) {
if(n == 0){
return 1;
}
int fn = n * fact(n-1);
return fn;
}
// Print Sum of first n natural numbers
public static int calcSum(int n){
if(n == 1){
return 1;
}
int Snm1 = calcSum(n-1);
int Sn = n + Snm1;
return Sn;
}
// Print Nth fibonacci Number
public static int fib(int n){
if(n == 0 || n == 1){
return n;
}
int fnm1 = fib(n-1);
int fnm2 = fib(n-2);
int fn = fnm1 + fnm2;
return fn;
}
// Check if a give array is sorted or not
public static boolean isSorted(int arr[], int i){
if(i == arr.length-1){
return true;
}
if(arr[i] > arr[i+1]){
return false;
}
return isSorted(arr, i+1);
}
// WAF to find the first Occurance of an element in an array
public static int firstOccurnace(int arr[], int key, int i){
if(i == arr.length){
return -1;
}
if(arr[i] == key){
return i;
}
return firstOccurnace(arr, key, i+1);
}
// WAF to find the last Occurance of an element in an array
public static int lastOccurance(int arr[], int key, int i){
if(i == arr.length){
return -1;
}
int isFound = lastOccurance(arr, key, i+1);
if(isFound == -1 && arr[i] == key){
return i;
}
return isFound;
}
// Print x^n
public static int power(int x, int n){
if(n == 0){
return 1;
}
int xnm1 = power(x, n-1);
int xn = x * xnm1;
return xn;
}
// Optimized x^n
public static int optimizedPower(int x, int n) {
if (n == 0) return 1;
int half = optimizedPower(x, n/2);
if (n % 2 == 0) {
return half * half;
} else {
return x * half * half;
}
}
// Tiling Problem
public static int tilingProblem(int n){
if(n == 0 || n == 1){
return 1;
}
// Vertical Choice
int fnm1 = tilingProblem(n-1);
// Horizontal Choice
int fnm2 = tilingProblem(n-2);
int totWays = fnm1 + fnm2;
return totWays;
}
// Remove Duplicates from a string
public static void removeDuplicates(String str, int idx, StringBuilder newStr, boolean map[]){
if(idx == str.length()){
System.out.println(newStr);
return;
}
char currChar = str.charAt(idx);
if(map[currChar-'a'] == true) {
removeDuplicates(str, idx+1, newStr, map);
} else {
map[currChar-'a'] = true;
removeDuplicates(str, idx+1, newStr.append(currChar), map);
}
}
// Friends Pairing Problem
public static int friendsPairing(int n){
if(n == 1 || n == 2){
return n;
}
return friendsPairing(n-1) + (n-1) * friendsPairing(n-2);
}
// Binary Strings Problem
public static void printBinStrings(int n, int lastPlace, String str){
if(n == 0){
System.out.println(str);
return;
}
printBinStrings(n-1, 0, str+"0");
if(lastPlace == 0){
printBinStrings(n-1, 1, str+"1");
}
}
public static void main(String[] args) {
// int n = 5;
// printDec(n);
// printInc(n);
// System.out.println(fact(n));
// System.out.println(calcSum(n));
// System.out.println(fib(n)); (https://chatgpt.com/s/t_68c3c11dfed88191a148d2e4d4bc8dc9)
// int arr[] = {8, 3, 6, 9, 5, 10, 2, 5, 3};
// System.out.println(isSorted(arr, 0));
// System.out.println(firstOccurnace(arr, 5, 0));
// System.out.println(lastOccurance(arr, 3, 0));
// System.out.println(power(2, 4));
// System.out.println(optimizedPower(2, 3));
// System.out.println(tilingProblem(2));
// https://chatgpt.com/s/t_68ca4d8b37208191ae600298bac0a539
// String str = "appnnacollege";
// removeDuplicates(str, 0, new StringBuilder(""), new boolean[26]);
// System.out.println(friendsPairing(3));
// https://chatgpt.com/s/t_68cd2e420c148191bff3707b687e702a
// printBinStrings(3, 0, "");
// https://chatgpt.com/s/t_68ce4d18191c8191ae24b3a455d31d35
}
}