-
Notifications
You must be signed in to change notification settings - Fork 9
/
13_06_Basic Problem Solving.js
187 lines (112 loc) · 2.27 KB
/
13_06_Basic Problem Solving.js
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
//#################################### Fibonacci 1 ################################
function fibo(n){
//base case
if(n==0 || n==1)
return n;
let res1 = fibo(n-1); //recursive call
let res2 = fibo(n-2); //recursive call
let res = res1+res2; //self work
return res;
}
console.log(fibo(5))
//#################################### Fibonacci Series #############################
function fibo(n){
//base case
if(n==0 || n==1)
return n;
let res1 = fibo(n-1); //recursive call
let res2 = fibo(n-2); //recursive call
let res = res1+res2; //self work
return res;
}
for(i=0;i<=5;i++){
console.log(fibo(i))
}
//######################### Array is sorted or not ############################
debugger;
function isSorted(arr,i){
//base case
if(i == arr.length-1){
return true;
}
//recursive call
let val = isSorted(arr,i+1);
//self work
if(val && arr[i]<arr[i+1]){
return true;
}
else{
return false;
}
}
console.log(isSorted([1,2,3,4,5],0))
//[1,2,3,4,5]
// ############ Subsequences ################
1
function PrintSubsequences(arr,index){
2
3
//base case
4
//[3] => 1. self element 2. empty element
5
if(index==arr.length-1){
6
let res=[];
7
res.push([arr[index]]);
8
res.push([]);
9
return res;
10
}
11
12
//Recursive call => [2,3]
13
let subSequences = PrintSubsequences(arr,index+1)
14
15
let sw = [];
16
for(i=0;i<subSequences.length;i++){
17
sw.push(subSequences[i]);
18
let val = [...subSequences[i]];
19
val.unshift(arr[index]);
20
sw.push(val);
21
}
22
return sw;
23
}
24
console.log(PrintSubsequences([2,3],0))
25
function PrintSubsequences(arr,index){
//base case
//[3] => 1. self element 2. empty element
if(index==arr.length-1){
let res=[];
res.push([arr[index]]);
res.push([]);
return res;
}
//Recursive call => [2,3]
let subSequences = PrintSubsequences(arr,index+1)
let sw = [];
for(i=0;i<subSequences.length;i++){
sw.push(subSequences[i]);
let val = [...subSequences[i]];
val.unshift(arr[index]);
sw.push(val);
}
return sw;
}
console.log(PrintSubsequences([2,3],0))