-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanswerExamples.js
201 lines (181 loc) · 5.33 KB
/
answerExamples.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//Question: Get fibonacci number by index:
//Solution 1 - recursion
function fib1(i){
if(i==0 || i==1)return i;
return fib1(i-1) + fib1(i-2);
}
//Solution 2 - loop
function fib2(i){
if(i==0 || i==1)return i;
var count=2, n1=0, n2=1;
while(count<=i){
count+=1;
const t = n2;
n2 = n1+n2;
n1 = t;
}
return n2;
}
//Question: Print fibonacci calling: printFib(n1, n2)
//Solution 1 - Descending order
function printFib1(n1, n2){
console.log(n1);
if(n1 > 0)printFib(n2-n1, n1);
}
//Solution 2 - Ascending order
function printFib2(n1, n2){
if(n1 > 0)printFib(n2-n1, n1);
console.log(n1);
}
//Question: Find missing number in array with values between 1...n (unordered)
//Solution 1 - Assume only one number is missing and Max value is known for example: 100
function findMissing1(arr){
var sum = 0;
for(const n of arr){
sum += n;
}
return (100*(100+1)/2) - sum;
}
//Solution 2 - Assume only one number is missing and Max value is unknown
function findMissing2(arr){
var sum = 0, max = 0;
for(const n of arr){
sum += n;
if(n > max) max = n;
}
return (max*(max+1)/2) - sum;
}
//Solution 3 - Assume 2 numbers are missing and Max value is unknown
function findMissing3(arr){
var sum = 0, max = 0, misSum, misNums;
for(const n of arr){
sum += n;
if(n > max) max = n;
}
misSum = (max*(max+1)/2) - sum;//Sum of two missing numbers
for(let n = Math.min(misSum, max-1); n > 1; n--){
if(arr.indexOf(n) < 0)misNums = [n, misSum-n];
}
return misNums;
}
//Solution 4 - Assume unknown amount of numbers are missing and Max value is unknown
function findMissing4(arr){
var sum = 0, count = 0, max = 0, misSum, misNums=[];
for(const n of arr){
count++;
sum += n;
if(n > max) max = n;
}
misSum = (max*(max+1)/2) - sum;//Sum of all missing numbers
count = max - count;//How many numbers are missing
for(let n = Math.min(misSum, max-1); n > 1; n--){
if(arr.indexOf(n) < 0){
misNums.push(n);
misSum -= n;
if(--count == 1){//only one left
misNums.push(misSum);
break;
}
}
}
return misNums;
}
//Solution 5 - Assume unknown amount of numbers are missing and Max value is unknown (using second array)
function findMissing5(arr){
var count=0, max = 0, neededNums=[], misNums=[];
for(const n of arr){
count++;
neededNums.push(0);//zero for every needed number
if(n > max) max = n;
}
for(let i = (max - count); i>0; i--){
neededNums.push(0);//complete zeros for missing numbers
}
for(const n of arr){//iterate again
neededNums[n == max ? 0 : n]++;//add one - used as index in second array (convert max to zero)
}
for(let i=neededNums.length; i>0; i--){
if(neededNums[i]<1)misNums.push(i);//if value is zero, than index is a missing number
}
return misNums;
}
//Question: find biggest contiguous sub array
function biggestSubArr(arr){
var max = Number.MIN_SAFE_INTEGER, sum = 0;
var test = [], found = [];
for(const n of arr){
sum += n;
test.push(n);
if(max < sum ){
max = sum;
found = test.slice(0);//clone existing test values to found
}
if(sum < 0){
sum = 0;
test = [];
}
}
return found;
}
//Question: Shuffle an array
function shuffle(arr){
var shuffle = arr.slice(0);
for(let i=shuffle.length-1; i >= 0; i--){
const r = Math.floor(Math.random() * i);
const t = shuffle[r];
shuffle[r] = shuffle[i];
shuffle[i] = t;
}
return shuffle;
}
//Question: count all digits in a number
//Solution 1 - dynamic type conversion String -> Number -> String
function countDigits1(num){
num = ""+num;//stringify the number
const digits = [0,0,0,0,0,0,0,0,0,0];
for(let i=0, count=num.length; i<count; i++){
digits[num[i]*1]++;
}
return digits;
}
//Solution 2 - without type conversion
function countDigits2(num){
const digits = [0,0,0,0,0,0,0,0,0,0];
for(let d; num > 0 && 0 <= (d = num%10); ){
digits[d]++;
num = parseInt(num / 10);
}
return digits;
}
//Question: Find duplicated int in array with values between 0...n (unordered). Cannot use additional memory.
//Solution 1 - Using sorting O(n^2).
function findDup1(arr){
arr.sort();//Bottle neck of this solution
for(let i=arr.length-1; i>=0; i--){
const t = arr[i];
if(t == arr[i-1])return t;
}
}
//Solution 2 - Without sorting O(n).
function findDup2(arr){
for(const n of arr){
const t = n<0 ? n*-1 : n;//if negative turn to positive
arr[t] *= -1;
}
for(const n of arr){
if(n > 0)return n;
}
}
//Question: Compress a string using the counts of repeated characters.
//Solution - Assume the string consist only from letters [A-Za-z] (no numbers)
function compress(str){
var compressed = "", strLength = str.length;
for(let i=0, compCount=0; i<strLength; i++){
compCount++;
if(i+1 >= strLength || str[i+1] != str[i]){
compressed += str[i]+compCount;
compCount = 0;
}
}
return compressed.length < strLength ? compressed : str;//if compressed isn't smaller - return original
}