-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
619 lines (493 loc) · 20.5 KB
/
Copy pathBinarySearch.java
File metadata and controls
619 lines (493 loc) · 20.5 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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
import java.util.Arrays;
public class BinarySearch {
int binarySearch(int[] arr, int l, int h, int val) {
if (l > h) return -1;
int mid = l + (h-l)/2;
if (arr[mid] == val) return mid;
if (val < arr[mid]) return binarySearch(arr, l, mid-1, val);
else return binarySearch(arr, mid+1, h, val);
}
int searchInsert(int[] nums, int trgt) {
int l = 0, r = nums.length-1;
while (l < r) {
int m = l + (r-l)/2;
if (nums[m] == trgt) return m;
else if (nums[m] < trgt) l = m+1;
else r = m;
}
return nums[l] <= trgt ? l+1 : l;
}
int floorSortedArray(int[] nums, int trgt) {
int l = 0, r = nums.length-1;
while (l < r-1) {
int m = l + (r-l)/2;
if (nums[m] > trgt) r = m-1;
else l = m;
}
return nums[r] <= trgt ? r : (nums[l] <= trgt ? l : -1);
}
// Question
// Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value.
int[] searchRange(int[] nums, int target) {
// Idea is to do two binary searches, one for first pos and another for last pos
// For first pos, we can have two cases
// if A[m] < target, then i = m+1, else if A[m] >= target then j = m
// when loop terminates, the value of i/j is where the range starts
// This is because, as we narrow our search range, we eventually reach a position when there are only two elems
// And then eventually our i/j become equal at the target elem, if it doesn't exist, then we can simply return -1
// Similar technique can be applied for last pos
// if A[m] <= target, then i = m, else if A[m] > target, then j = m-1
// Here the only change is the way we calculate m = (i+j)/2 + 1
// This is because, when A[m]==target, we set i=m which would keep looping forever, as the old way of calculating m is biased toward i
int[] res = new int[2];
// calculating first pos
int l = 0, r = nums.length-1;
while (l < r) {
int m = l + (r-l)/2;
if (target > nums[m]) l = m+1;
else if (target <= nums[m]) r = m;
}
if (nums[l] != target) return new int[]{-1, -1};
else res[0] = l;
// calculating last pos
r = nums.length-1;
while (l < r) {
int m = l + (r-l)/2 + 1;
if (target < nums[m]) r = m-1;
else if (target >= nums[m]) l = m;
}
res[1] = r;
return res;
}
int peakElement(int[] nums) {
// O(n) logic
// for (int i = 0; i < nums.length-1; i++) {
// if (nums[i] > nums[i+1]) return i;
// }
// return nums.length-1;
// Idea is based on the fact that the array will contain ascending(rising) range and descending(falling) range
// Therefore on which range our mid elem lies we can reduce our search space
// If mid is greater then next elem, then it lies on falling slope, so peak lies to left of mid
// If mid is less than next elem, then it lies on rising slope, so peak lies to the right
// At the end we are just left with one elem which is our peak elem
int l = 0, r = nums.length-1;
while (l < r) {
int m = l + (r-l)/2;
if (nums[m] > nums[m+1]) r = m;
else if (nums[m] < nums[m+1]) l = m+1;
}
return l;
}
int searchRotatedSortedArray(int[] nums, int target) {
// Idea is to use modified binary search
// After checking with mid elem, we try to find which part is sorted
// If left part is sorted, we check if target lies in left, no, then we move to right
// If not, then right part is sorted, so again we check if target lies in right, if not, we move to left
int l = 0, r = nums.length-1;
while (l <= r) {
int m = l + (r-l)/2;
if (nums[m] == target) return m;
// check if left part sorted
if (nums[l] <= nums[m]) {
if (nums[l] <= target && target < nums[m]) r = m-1;
else l = m+1;
} else { // right sorted
if (nums[m] < target && target <= nums[r]) l = m+1;
else r = m-1;
}
}
return -1;
}
// Problem similar to first but array has duplicates
boolean searchRotatedSortedArray2(int[] nums, int target) {
// Idea is to divide the search array into two parts based on rotated index
// and check in which part of array target and mid index lies
// Accordingly we adjust the left and right indices
// Edge case is when mid equals left, that means elem can lie in both parts
// So we need to increment the left index
// Therefore due to this, we have worst case TC O(N), this happens when all elems are same and target doesn't exist
int l = 0, r = nums.length-1;
while (l <= r) {
int m = l + (r-l)/2;
if (nums[m] == target) return true;
// check if binary search is helpful
if (nums[l] == nums[m]) {
l++;
continue;
}
// check if mid and target exist in first part of array
boolean mIndex = nums[l] <= nums[m];
boolean tIndex = nums[l] <= target;
if (mIndex ^ tIndex) { // both mid and target lie in diff parts
if (mIndex) l = m+1;
else r = m-1;
} else {
if (target > nums[m]) l = m+1;
else r = m-1;
}
}
return false;
}
// array contains unique elems
int findMinRotatedSortedArray(int[] nums) {
int l = 0, r = nums.length-1;
// if no rotation
if (nums[l] < nums[r]) return nums[l];
while (l < r) {
int m = l + (r-l)/2;
if (nums[m] == nums[0]) l++; // increment since we dont know which part min lies
else if (nums[m] > nums[0]) l = m+1; // min lies on right part
else r = m; // min lies on left part
}
return nums[l];
}
// [1,1,2,3,3,4,4,8,8]
int singleNonDuplicate(int[] nums) {
// Idea to use position of mid to determine whether to go left or right
int l = 0, r = nums.length-1;
while (l < r) {
int m = l + (r-l)/2;
if ((m%2 == 0 && nums[m] == nums[m+1]) || (m%2 == 1 && nums[m-1] == nums[m])) l = m+1;
else r = m;
}
return nums[l];
}
int[] findPeakGrid(int[][] mat) {
// Idea is to apply binary search on either row or col
// So once we get midRow or midCol, we search for maxElem in that midRange
// Then we check if neighbors are larger then this max elem, if so we towards starr or end range
int startRow = 0, endRow = mat.length-1;
while (startRow <= endRow) {
int midRow = startRow + (endRow-startRow)/2;
int maxCol = 0;
for (int col = 0; col < mat[0].length; col++) {
maxCol = mat[midRow][col] > mat[midRow][maxCol] ? col : maxCol;
}
boolean topIsBig = midRow - 1 >= startRow && mat[midRow-1][maxCol] > mat[midRow][maxCol];
boolean bottomIsBig = midRow + 1 <= endRow && mat[midRow+1][maxCol] > mat[midRow][maxCol];
if (!topIsBig && !bottomIsBig) return new int[]{midRow, maxCol};
if (topIsBig) endRow = midRow-1;
else if (bottomIsBig) startRow = midRow+1;
}
return null;
}
int medianSortedRowMatrix(int[][] mat) {
// Idea lies in the fact that all matrix numbers are present in a certain range
// This range will be our search space for binary search algo
// When we keep count of how many no's are <= curr no, we observe a monotonically increasing search space
// To implement algo find how many elems in matrix are <= mid elem
// Once we find that, in order to get the median, we need to shrink our search space
// Thus based on where our median can lie, we can move to either or right
// When low crosses high, that val of low is our median elem
int m = mat.length, n = mat[0].length;
int low = 0, high = 2000;
while (low <= high) {
int mid = low + (high-low)/2;
int cnt = 0;
for (int i = 0; i < m; i++) cnt += elemsLessThanEqualToMid(mat[i], mid);
if (cnt <= (m*n)/2) low = mid+1;
else high = mid-1;
}
return low;
}
int elemsLessThanEqualToMid(int[] arr, int target) {
int l = 0, r = arr.length-1;
while (l <= r) {
int m = l + (r-l)/2;
if (arr[m] <= target) l = m+1;
else r = m-1;
}
return l;
}
long squareRootNumber(long x) {
// Binary Search can be implemented to any search space which is monotonic in nature
// So it can be a increasing sequence or decreasing sequence
// Our search space lies between [1, x], where everytime we find the mid
// If mid*mid > x, we move towards left else we move towards right
long low = 1, high = x;
while (low < high-1) {
long mid = low + (high-low)/2;
if (mid*mid >= x) high = mid;
else low = mid;
}
return (low*low <= x && x < high*high) ? low : high;
}
int nthRootNumber(int n, int m) {
// Similar idea as above
int low = 1, high = m;
while (low < high - 1) {
int mid = low + (high-low)/2;
if (multiplyNTimes(mid, n) < m) low = mid;
else high = mid;
}
return (multiplyNTimes(low, n) <= m && multiplyNTimes(high, n) < m) ? low : high;
}
int multiplyNTimes(int num, int n) {
long res = 1;
int maxVal = 1_000_000_009;
for (int i = 0; i < n; i++) {
if (res*num > maxVal) return maxVal;
res *= num;
}
return (int) res;
}
int minEatingSpeed(int[] piles, int h) {
// Idea is to use the fact that value if k(min eating speed) will lie in a certain range
// Thus we can apply binary search to find this k
// max value of range will be the max no of bananas in pile
// Everytime, for each mid, we check if for this value, koko can eat all bananas in h hours
// Based on this, we move towards left or right
int low = 1, high = 0;
for (int pile : piles) high = Math.max(pile, high);
while (low < high) {
int mid = low + (high-low)/2;
if (canEatAll(piles, mid, h)) high = mid;
else low = mid+1;
}
return low;
}
boolean canEatAll(int[] piles, int k, int h) {
int hourCnt = 0;
for (int pile : piles) {
hourCnt += pile / k;
if (pile % k == 0) hourCnt++;
}
return hourCnt <= h;
}
int minDaysToMakeBouquets(int[] bloomDay, int m, int k) {
// Some pointers to consider here
// 1. Flowers picked for each bouquet must be adjacent
// 2. Min days will lie between [min bloom days for flower, max bloom days for flower]
// 3. Thus we have a search range and we can apply binary search
// 4. For each mid value, we need to see if we can create the bouquets in mid days
int n = bloomDay.length;
if (k > n || m > n || m*k > n) return -1;
int low = 1_000_000_009, high = 1;
for (int day : bloomDay) {
low = Math.min(day, low);
high = Math.max(day, high);
}
while (low < high) {
int mid = low + (high-low)/2;
if (canMakeBouquets(bloomDay, mid, m, k)) high = mid;
else low = mid+1;
}
return low;
}
boolean canMakeBouquets(int[] bloomDays, int days, int m, int k) {
int flowers = 0, bouquetCnt = 0;
for (int i = 0; i < bloomDays.length; i++) {
if (bloomDays[i] > days) flowers = 0;
else if (++flowers >= k) {
bouquetCnt++;
flowers = 0;
}
}
return bouquetCnt >= m;
}
int smallestDivisor(int[] nums, int threshold) {
int low = 1, high = 0, n = nums.length;
for (int num : nums) high = Math.max(num, high);
while (low < high) {
int mid = low + (high-low)/2;
int currThresh = 0;
for (int i = 0; i < n; i++) {
if (nums[i]%mid == 0) currThresh += nums[i]/mid;
else currThresh += nums[i]/mid + 1;
}
if (currThresh <= threshold) high = mid;
else low = mid+1;
}
return low;
}
int shipWithinDays(int[] weights, int days) {
// Idea is to apply binary search on range of weights
int low = 0, high = 0;
for (int weight : weights) {
low = Math.max(low, weight);
high += weight;
}
while (low < high) {
int mid = low + (high-low)/2;
int currWeight = 0, currDays = 1;
for (int weight : weights) {
if (currWeight + weight > mid) {
currDays++;
currWeight = 0;
}
currWeight += weight;
}
if (currDays <= days) high = mid;
else low = mid+1;
}
return low;
}
double medianSortedArrays(int[] nums1, int[] nums2) {
if (nums2.length < nums1.length) return medianSortedArrays(nums2, nums1);
int n1 = nums1.length, n2 = nums2.length;
int low = 0, high = n1;
while (low <= high) {
int mid1 = low + (high-low)/2;
int mid2 = (n1+n2+1)/2 - mid1;
int l1 = (mid1 == 0) ? Integer.MIN_VALUE : nums1[mid1-1];
int l2 = (mid2 == 0) ? Integer.MIN_VALUE : nums2[mid2-1];
int r1 = (mid1 == n1) ? Integer.MAX_VALUE : nums1[mid1];
int r2 = (mid2 == n2) ? Integer.MAX_VALUE : nums2[mid2];
if (l1 <= r2 && l2 <= r1) {
if ((n1+n2)%2 == 0) return (Math.max(l1, l2) + Math.min(r1, r2))/2.0;
else return Math.max(l1, l2);
} else if (l1 > r2) high = mid1-1;
else low = mid1+1;
}
return 0.0;
}
int aggresiveCows(int[] stalls, int n, int k) {
// Idea is to apply binary search on the range of possible distances
// Main objective is that min distance between any two cows is the max possible dist
// Since the stalls positions are kind of like coordinates, we can sort the array to easily place cows
// Now when checking whether we can place cows using mid as minDist, we can do that by placing first cow always at the start
// This is kind of like greedy way and then we check if we can place all cows satisfying this dist
Arrays.sort(stalls);
int res = 0;
int low = 1, high = stalls[n-1];
while (low <= high) {
int mid = low + (high-low)/2;
int lastPlacedCow = stalls[0], cows = 1;
for (int i = 1; i < n; i++) {
if (stalls[i] - lastPlacedCow >= mid) {
cows++;
lastPlacedCow = stalls[i];
}
}
if (cows >= k) {
low = mid+1;
res = mid;
}
else high = mid-1;
}
return res;
}
int findMinimumPages(int[] books, int m) {
// Objective is to max pages alloted to a student which is min among all other permutations
if (m > books.length) return -1;
int low = 1_000_001, high = 0;
for (int book : books) {
low = Math.min(book, low);
high += book;
}
int res = 0;
while (low <= high) {
int mid = low + (high-low)/2;
if (isAllocationPossible(books, mid, m)) {
res = mid;
high = mid-1;
} else low = mid+1;
}
return res;
}
boolean isAllocationPossible(int[] books, int maxPages, int totalStudents) {
int students = 1, pages = 0;
for (int book : books) {
if (book > maxPages) return false;
if (pages + book > maxPages) {
students++;
pages = 0;
}
pages += book;
}
return students <= totalStudents;
}
int splitArray(int[] nums, int k) {
// Idea is similar to previous problem
// Here we will apply binary search on range of possible sum
int low = 0, high = 0;
for (int num : nums) {
low = Math.max(low, num);
high += num;
}
int res = 0;
while (low <= high) {
int mid = low + (high-low)/2;
if (isSplitPossible(nums, k, mid)) {
res = mid;
high = mid-1;
} else low = mid+1;
}
return res;
}
boolean isSplitPossible(int[] nums, int k, int maxSum) {
int subArrCnt = 1, currSum = 0;
for (int num : nums) {
if (num > maxSum) return false;
if (currSum + num > maxSum) {
subArrCnt++;
currSum = 0;
}
currSum += num;
}
return subArrCnt <= k;
}
int findKthPositive(int[] arr, int k) {
// Idea is to apply binary search on search space of all possible integers
// Once we find the mid, we check which no in arr is just less than mid
// And then calculate pos of this mid in actual range
int low = 1, high = 2000, n = arr.length;
while (low < high) {
int mid = low + (high-low)/2;
int l = 0, r = n-1;
while (l < r) {
int m = l + (r-l)/2 + 1;
if (arr[m] > mid) r = m-1;
else l = m;
}
System.out.println("mid: "+mid+", arr val: "+arr[l]);
int pos = (arr[l] > mid) ? mid : mid-l-1;
if (pos < k) low = mid+1;
else high = mid;
System.out.println("low: "+low+", high: "+high);
}
return low;
}
double smallestMaxDistanceGasStation(int[] stations, int k) {
// Objective is to add k more gas stations so that max dist between gas stations is minimized
// Idea is to apply binary search on range of possible distances
// For each mid dist, we need to check how many stations we must add to make dist btwn adjacent stations <= mid val
// Also we set both high and low values to mid, because if we make low = mid+1, we might skip on ans as diff btwn them is very small
int n = stations.length;
double low = 0.0, high = stations[n-1] - stations[0];
while ((high - low) > 1e-6) {
double mid = low + (high - low)/2.0;
int newStations = 0;
for (int i = 0; i < n-1; i++) {
newStations += Math.floor((stations[i+1]-stations[i])/mid);
}
if (newStations <= k) high = mid;
else low = mid;
}
return Math.round(high*100) / 100.0;
}
long kthElementSortedArrays(int[] arr1, int[] arr2, int k) {
// Idea is similar to finding median of two sorted arrays
// In median case, our k was (n1+n2)/2
if (arr1.length > arr2.length) return kthElementSortedArrays(arr2, arr1, k);
int n1 = arr1.length, n2 = arr2.length;
int low = Math.max(0, k-n2), high = Math.min(n1, k);
while (low <= high) {
int mid1 = low + (high-low)/2;
int mid2 = k - mid1;
int l1 = (mid1 == 0) ? Integer.MIN_VALUE : arr1[mid1-1];
int l2 = (mid2 == 0) ? Integer.MIN_VALUE : arr2[mid2-1];
int r1 = (mid1 == n1) ? Integer.MAX_VALUE : arr1[mid1];
int r2 = (mid2 == n2) ? Integer.MAX_VALUE : arr2[mid2];
if (l1 <= r2 && l2 <= r1) return Math.max(l1, l2);
else if (l1 > r2) high = mid1-1;
else low = mid1+1;
}
return 0;
}
public static void main(String[] args) {
BinarySearch bSearch = new BinarySearch();
int[] stations = new int[]{1,2,3,4,5,6,7,8,9,10};
System.out.println(bSearch.smallestMaxDistanceGasStation(stations, 9));
}
}