-
Notifications
You must be signed in to change notification settings - Fork 1
/
Recursion.java
176 lines (172 loc) · 4.57 KB
/
Recursion.java
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
package Java;
//import java.util.*;
public class Recursion {
public static void conscutive(int n, String str, int last){
//Base
if(n == 0){
System.out.println(str);
return;
}
conscutive(n-1, str+'0', 0);
if(last == 0){
conscutive(n-1, str+'1', 1);
}
}
public static int friendpaired(int n){
//Base
if(n == 1 || n == 2){
return n;
}
//Single
int single = friendpaired(n-1);
//paired
int paired = (n-1) * friendpaired(n-2);
int total = single + paired;
return total;
}
public static void removeDuplicate(boolean map[], StringBuilder newStr, int idx, String str ){
if(idx == str.length()){
System.out.println(newStr);
return;
}
char currElement = str.charAt(idx);
if(map[currElement - 'a'] == true){
removeDuplicate(map, newStr, idx+1, str);
}
else{
map[currElement - 'a'] = true;
removeDuplicate(map, newStr.append(currElement), idx+1, str);
}
}
public static int tiling(int n){
//base case
if(n == 0 || n == 1){
return 1;
}
//kaam
//vertical
int fnm1 = tiling(n-1);
//horizontal
int fnm2 = tiling(n-2);
int total = fnm1 + fnm2;
return total;
}
public static int optimzPower(int a, int n){
if(n == 0){
return -1;
}
int halfPow = optimzPower(a, n/2);
int halfPowSq = halfPow * halfPow;
// n = odd
if(n % 2 != 0){
halfPowSq = a * halfPowSq;
}
return halfPowSq;
}
public static int power(int x, int n){
if(n == 0){
return 1;
}
int xtmn = power(x,n-1);
int xn = x * xtmn;
return xn;
}
public static void allOccurs(int arr[], int key, int i){
if(i == arr.length){
return;
}
if(arr[i] == key){
System.out.print(i+" ");
}
allOccurs(arr, key, i+1);
}
public static int lastOccurs(int arr[], int key, int i){
if(i == arr.length){
return -1;
}
int lastFound = lastOccurs(arr, key, i+1);
if(lastFound == -1 && arr[i] == key){
return i;
}
return lastFound;
}
public static int firstOccurs(int arr[], int key, int i){
if(i == arr.length){
return -1;
}
if(arr[i] == key){
return i;
}
return firstOccurs(arr, key, i+1);
}
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);
}
public static int fabinachi(int num){
if(num == 0 || num == 1){
return num;
}
int fnm1 = fact(num -1);
int fnm2 = fact(num - 2);
int fb = fnm1 + fnm2;
return fb;
}
public static int sum(int n){
if(n == 1){
return 1;
}
int Snm1 = sum(n-1);
int total = n + Snm1;
return total;
}
public static int fact(int num){
if(num == 0){
return 1;
}
int fnm1 = fact(num-1);
int fn = num * fnm1;
return fn;
}
public static void cout(int num){
if(num == 1){
System.out.print("1");
return;
}
cout(num-1);
System.out.print(num);
}
public static void printing(int num){
if(num == 1){
System.out.print("1");
return;
}
System.out.print(num + " ");
printing(num - 1);
}
public static void main(String args[]) {
//cout(10);
System.out.println(fact(5));
System.out.println(sum(10));
System.out.println(fabinachi(4));
int arr[] = {5,9,5,10,5,15,16,8};
System.out.println(isSorted(arr, 0));
System.out.println(firstOccurs(arr, 8, 0));
System.out.println(lastOccurs(arr, 5, 0));
System.out.println(power(25, 0));
System.out.println(optimzPower(5, 2));
System.out.println(tiling(4));
StringBuilder newStr = new StringBuilder("");
boolean map[] = new boolean[26];
String str = "kessshhavv";
removeDuplicate(map, newStr, 0, str);
System.out.println(friendpaired(7));
conscutive(5, "", 0);
allOccurs(arr, 8, 0);
}
}