-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11-modified-binary-search.rb
333 lines (294 loc) · 7 KB
/
11-modified-binary-search.rb
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
puts "Search in sorted array - order agnostic"
def search(nums, key)
first, last = 0, nums.length - 1
while first <= last
mid = (first + last) / 2
return mid if nums[mid] == key
if nums[0] < nums[-1] && key > nums[mid] || nums[0] > nums[-1] && key < nums[mid]
first = mid + 1
else
last = mid - 1
end
end
-1
end
p search [4, 6, 10], 10
p search [10, 6, 4], 4
puts "Ceiling"
def ceiling(nums, key)
first, last = 0, nums.length - 1
while first < last
mid = (first + last) / 2
if nums[mid] < key
first = mid + 1
else
last = mid
end
end
nums[first] >= key ? first : -1
end
p ceiling [4, 6, 10], 6
p ceiling [1, 3, 8, 10, 15], key = 12
p ceiling [4, 6, 10], 17
puts "Floor"
def floor(nums, key)
first, last = 0, nums.length - 1
return -1 if nums[0] > key
while first <= last
mid = (first + last) / 2
if nums[mid] > key
last = mid - 1
elsif nums[mid] < key
first = mid + 1
else
return mid
end
end
last
end
p floor [4, 6, 10], 6
p floor [1, 3, 8, 10, 15], key = 12
p floor [4, 6, 10], 17
def floor(nums, key)
first, last = 0, nums.length - 1
while first < last
mid = (first + last + 1) / 2
if nums[mid] > key
last = mid - 1
else
first = mid
end
end
nums[first] <= key ? first : -1
end
p floor [4, 6, 10], 6
p floor [1, 3, 8, 10, 15], key = 12
p floor [4, 6, 10], 7
puts "First Letter"
def first_letter(chars, key)
first, last = 0, chars.length - 1
while first <= last
mid = (first + last) / 2
if chars[mid].ord <= key.ord
first = mid + 1
else
last = mid - 1
end
end
chars[first % chars.length]
end
p first_letter ["a", "c", "f", "h"], key = "f"
p first_letter ["a", "c", "f", "h"], key = "b"
p first_letter ["a", "c", "f", "h"], key = "m"
p first_letter ["a", "c", "f", "h"], key = "h"
puts "number range"
def number_range(nums, key)
first, last = 0, nums.length - 1
while first <= last
mid = (first + last) / 2
if nums[mid] > key
first = mid + 1
elsif nums[mid] < key
last = mid - 1
else
break
end
end
return [-1, -1] if first > last
first, last = (first + last) / 2, (first + last) / 2
first -= 1 while first >= 0 && nums[first] == key
last += 1 while last < nums.length && nums[last] == key
[first + 1, last - 1]
end
def number_range(nums, key)
[bin_search_idx(nums, key, false), bin_search_idx(nums, key, true)]
end
def bin_search_idx(nums, key, find_max_idx = true)
first, last = 0, nums.length - 1
key_index = -1
while first <= last
mid = (last + first) / 2
if nums[mid] > key
last = mid - 1
elsif nums[mid] < key
first = mid + 1
else
key_index = mid
if find_max_idx
first = mid + 1
else
last = mid - 1
end
end
end
key_index
end
p number_range [4, 6, 6, 6, 9], key = 6
p number_range [1, 3, 8, 10, 15], 10
p number_range [1, 3, 8, 10, 15], 12
puts "Find in infinite sorted array"
def find_in_infinite_array(arr, key)
last = 1
last *= 2 while arr[last - 1] < key
first = 0
while first <= last
mid = (first + last) / 2
return mid if arr[mid] == key
if arr[mid] > key
last = mid - 1
else
first = mid + 1
end
end
-1
end
p find_in_infinite_array [4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30], key = 16
puts "Element with minimum diff"
def min_diff(nums, key)
first, last = 0, nums.length - 1
while first < last
mid = (first + last) / 2
diff = (nums[mid] - key).abs
return nums[mid] if diff.zero?
if (nums[mid + 1] - key).abs > diff
last = mid
else
first = first + 1
end
end
nums[first]
end
def min_diff(nums, key)
first, last = 0, nums.length - 1
while first <= last
mid = (first + last) / 2
if nums[mid] < key
first = mid + 1
elsif nums[mid] > key
last = mid - 1
else
return nums[mid]
end
end
first_diff = first < nums.length ? nums[first] : 1.0 / 0
last_diff = last >= 0 ? nums[last] : 1.0 / 0
[last_diff, first_diff].min
end
p min_diff [4, 6, 10], 7
p min_diff [4, 6, 10], key = 4
p min_diff [1, 3, 8, 10, 15], key = 12
p min_diff [4, 6, 10], key = 17
puts "Max in bitonic array"
def max_in_bitonic(nums)
first, last = 0, nums.length - 1
while first < last
mid = (first + last) / 2
if nums[mid] < nums[mid + 1]
first = mid + 1
elsif nums[mid] > nums[mid + 1]
last = mid
end
end
nums[first]
end
p max_in_bitonic [1, 3, 8, 12, 4, 2]
p max_in_bitonic [3, 8, 3, 1]
p max_in_bitonic [1, 3, 8, 12]
p max_in_bitonic [10, 9, 8]
puts "Search in bitonic"
def search_in_bitonic(arr, key)
first, last = 0, arr.length - 1
while first < last
mid = (first + last) / 2
if arr[mid] > arr[mid + 1]
last = mid
else
first = mid + 1
end
end
left_idx = bin_search(arr, 0, mid, key)
return left_idx if left_idx >= 0
bin_search(arr, mid + 1, arr.length - 1, key, true)
end
def bin_search(arr, first, last, key, descending = false)
while first <= last
mid = (first + last) / 2
return mid if arr[mid] == key
if (!descending && arr[mid] > key) || (descending && arr[mid] < key)
last = mid - 1
else
first = mid + 1
end
end
-1
end
p search_in_bitonic [1, 3, 8, 4, 3], key = 4
p search_in_bitonic [3, 8, 3, 1], key = 8
p search_in_bitonic [1, 3, 8, 12], key = 12
p search_in_bitonic [10, 9, 8], key = 10
puts "Search in rotated array"
def search_in_rotated(arr, key)
first, last = 0, arr.length - 1
while first < last
mid = (first + last) / 2
if arr[mid] > arr[mid + 1]
first = last = mid
elsif arr[mid] >= arr[first]
if arr[mid] == arr[first] && arr[mid] == arr[last]
first += 1
last -= 1
else
first = mid + 1
end
else
last = mid - 1
end
end
left_idx = bin_search(arr, 0, first, key)
return left_idx if left_idx >= 0
bin_search(arr, first + 1, arr.length - 1, key)
end
def search_in_rotated(arr, key)
first, last = 0, arr.length - 1
while first <= last
mid = (first + last) / 2
return mid if arr[mid] == key
if arr[mid] == arr[last] && arr[mid] == arr[first]
last -= 1
first += 1
elsif arr[mid] >= arr[first]
if (arr[first]..arr[mid]).include?(key)
last = mid - 1
else
first = mid + 1
end
elsif arr[last] >= arr[mid]
if (arr[mid]..arr[last]).include?(key)
first = mid + 1
else
last = mid - 1
end
end
end
-1
end
p search_in_rotated [10, 15, 1, 3, 8], 15
p search_in_rotated [4, 5, 7, 9, 10, -1, 2], key = 10
p search_in_rotated [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], key = 10
p search_in_rotated [1, 1, 1, 1, 0, 1, 1, 1, 1], 0
puts "Rotation count"
def rotation_count(arr)
first, last = 0, arr.length - 1
while first < last
mid = (first + last) / 2
return mid + 1 if arr[mid] > arr[mid + 1]
if arr[mid] > arr[last]
first = mid + 1
else
last = mid
end
end
first
end
p rotation_count [10, 15, 1, 3, 8]
p rotation_count [4, 5, 7, 9, 10, -1, 2]