-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSA2_binary_Search.java
More file actions
527 lines (390 loc) · 13.5 KB
/
DSA2_binary_Search.java
File metadata and controls
527 lines (390 loc) · 13.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
import java.util.Arrays;
public class DSA2_binary_Search {
public static void main(String[] args) {
/* // for 1
int arr [] = {-24 ,-20 ,- 14 , -8 , 0 , 9 , 12, 14 , 16 , 24 , 36 ,48};
int target = 14 ;
int ans = binary_Search( arr , target);
System.out.println(ans);
*/
/*
// for 2
int arr [] = {-24 ,-20 ,- 14 , -8 , 0 , 9 , 12, 14 , 16 , 24 , 36 ,48};
int target = 14 ;
int arr1 [] = {48 , 35, 29 ,27 ,23 ,17 ,18 , 9 ,3 , -1 , -4};
int target1 = 23;
int ans = binary_SearchFor_unknown_assending_or_not( arr , target);
System.out.println(ans);
int ans1 = binary_SearchFor_unknown_assending_or_not( arr1 , target1);
System.out.println(ans1);
*/
// for 3 and 4 :
/*int target = 15;
int arr [] = {-24 ,-20 ,- 14 , -8 , 0 , 9 , 12, 14 , 16 , 24 , 36 ,48};
int ans = binary_Ceiling_Search( arr , target);
System.out.println(ans);
int ans1 = binary_floor_Search( arr , target);
System.out.println(ans1);
*/
// for 5 :-
/*
int [] numbers = {5,7,7,7,7,7,7,8,45,7,20,56};
int target = 7;
String ans = startrange(numbers, target);
System.out.println(ans);
*/
// for 6:
/*int arr [] = {-24 ,-20 ,- 14 , -8 , 0 , 9 , 12, 14 , 16 , 24 , 36 ,48};
int target = 48 ;
System.out.println(ans(arr, target));
*/
// for 7 :-
/*int [] mountain = {2,4,6,8,10,12,15,23,9,7,5,3,1};
int ans = Mountain(mountain);
System.out.println(ans);
*/
// for 8 :-
/*
int [] mountain = {2,4,6,8,10,12,15,23,9,7,5,3,1};
int ans = target_Find(mountain, 7 );
System.out.println(ans);
*/
// for 9:-
/*int [] rotation = {3,4,5,6 ,7,0,1,2};
// for non repeated element
System.out.println(FindRotation(rotation , true));
int [] dup_rot = {3,4,5,6,6,7,0,1,2,2};
System.out.println(FindRotation(dup_rot, false));
*/
}
//Question 1 :-
/*static int binary_Search (int arr [] ,int target) {
int start = 0;
int end = arr.length-1 ;
while(start <= end) {
int mid = start + (end - start) / 2;
if (target < arr[mid]){
end = mid -1 ;
}
else if (target > arr[mid]){
start = mid + 1 ;
}
else{
return mid;
}
}
return -1;
}*/
// Question 2 :-
/*static int binary_SearchFor_unknown_assending_or_not (int arr [] ,int target) {
int start = 0;
int end = arr.length-1 ;
boolean isAsending = (arr[start]< arr[end]);
while(start <= end) {
int mid = start + (end - start) / 2;
if ( target == arr[mid]){
return mid ;
}
if (isAsending){
if (target < arr[mid]){
end = mid -1 ;
}
else {
start = mid + 1 ;
}
}
else {
if (target > arr[mid]){
end = mid -1 ;
}
else {
start = mid + 1 ;
}
}
}
return -1;
}*/
// Question 3 :- find ceiling(smaller number that is >=target) og target number
/* static int binary_Ceiling_Search (int arr [] ,int target) {
int start = 0;
int end = arr.length-1 ;
while(start <= end) {
int mid = start + (end - start) / 2;
if (target > arr[arr.length-1]){ // for if target is larger than the highest number
return -1;
}
if (target < arr[mid]){
end = mid -1 ;
}
else if (target > arr[mid]){
start = mid + 1 ;
}
else{
return mid;
}
}
return start;
}
// Question 4 :- find floor(grater number that is <=target) og target number
static int binary_floor_Search (int arr [] ,int target) {
int start = 0;
int end = arr.length-1 ;
while(start <= end) {
int mid = start + (end - start) / 2;
if (target > arr[arr.length-1]){ // for if target is larger than the highest number
return -1;
}
if (target < arr[mid]){
end = mid -1 ;
}
else if (target > arr[mid]){
start = mid + 1 ;
}
else{
return mid;
}
}
return end;
}*/
// Question 5 :- find last and first index of requtired target
/*
public static String startrange (int [] numbers , int target){
int [] ans = {-1 ,-1};
int start = search(numbers , target ,true);
int end = search(numbers, target, false);
ans[0]= start;
ans[1] = end;
String s = Arrays.toString(ans);
return s ;
}
static int search(int [] arr ,int target ,boolean is_first_index){
int ans = -1;
int start = 0;
int end = arr.length-1 ;
while(start <= end) {
int mid = start + (end - start) / 2;
if (target < arr[mid]){
end = mid -1 ;
}
else if (target > arr[mid]){
start = mid + 1 ;
}
else{
ans = mid ;
if (is_first_index){
end = mid -1;
}
else{
start = mid +1;
}
}
}
return ans;
}*/
//Question 6 :- find target in infinite length array no useing .length
/*static int ans (int [] arr , int target){
int start = 0;
int end = 1;
while (target>arr[end]) {
int newStart = end +1;
// this end is my because length out of bound error
end = end + (end -start + 1) ;
//original
// end = end +(end - start +1)*2 ;
start = newStart ;
}
return search(arr, target, start, end);
}
static int search(int [] arr ,int target , int start , int end ){
while(start <= end) {
int mid = start + (end - start) / 2;
if (target < arr[mid]){
end = mid -1 ;
}
else if (target > arr[mid]){
start = mid + 1 ;
}
else{
return mid;
}
}
return -1;
}*/
// Question 7 :- find the peck of the mountain array
/*static int Mountain (int [] mountain){
int start = 0;
int end = mountain.length-1;
while (start < end) {
int mid = start + (end -start) / 2 ;
if (mountain[mid] > mountain[mid+1]){
// you in decreasing side or mid is the element
end = mid ;
}
else{
// you are in the incrasing side of the array
// mid is not the peaknumber
start = mid +1 ;
}
}
// both start and end are moving towards the peak number then any can we return start or end
return start ;
}*/
//Question 8 :- find the element in mountain array
/* static int target_Find (int mountain [] , int target){
int peak = Mountain(mountain);
int First = binary_SearchFor_unknown_assending_or_not(mountain, target, 0, peak);
if (First != -1 ){
return First ;
}
return binary_SearchFor_unknown_assending_or_not(mountain, target, peak+1, mountain.length-1);
}
static int Mountain (int [] mountain){
int start = 0;
int end = mountain.length-1;
while (start < end) {
int mid = start + (end -start) / 2 ;
if (mountain[mid] > mountain[mid+1]){
// you in decreasing side or mid is the element
end = mid ;
}
else{
// you are in the incrasing side of the array
// mid is not the peaknumber
start = mid +1 ;
}
}
// both start and end are moving towards the peak number then any can we return start or end
return start ; // peak index
}
static int binary_SearchFor_unknown_assending_or_not (int arr [] ,int target , int start ,int end) {
boolean isAsending = (arr[start]< arr[end]);
while(start <= end) {
int mid = start + (end - start) / 2;
if ( target == arr[mid]){
return mid ;
}
if (isAsending){
if (target < arr[mid]){
end = mid -1 ;
}
else {
start = mid + 1 ;
}
}
else {
if (target > arr[mid]){
end = mid -1 ;
}
else {
start = mid + 1 ;
}
}
}
return -1;
}*/
// Question 9 :- find rotation of array
/*static int FindRotation(int[] arr , boolean duplicate){
if (duplicate){
int pivot = FindPivot(arr);
return pivot+1 ;
}
else if (!duplicate){
int pivot = FindPivotIn_Duplicate(arr);
return pivot+1;
}
return 0 ;
}
// for non duplicate containing array
static int FindPivot (int [] arr){
int start = 0;
int end = arr.length-1;
while(start<= end){
int mid = start + (end -start)/2 ;
//case1
if (mid < end && arr[mid]>arr[mid+1]){
return mid ;
} //case2
if (mid > start && arr[mid]<arr[mid -1]){
return mid-1;
} //case 3
if (arr[start] >= arr[mid]){
end = mid-1;
}
//case4 start element < mid element
else {
start = mid +1;
}
}
return -1 ;
}
// for duplicate containing array
static int FindPivotIn_Duplicate (int [] arr){
int start = 0;
int end = arr.length-1;
while(start<= end){
int mid = start + (end -start)/2 ;
//case1
if (mid < end && arr[mid]>arr[mid+1]){
return mid ;
} //case2
if (mid > start && arr[mid]<arr[mid -1]){
return mid-1;
} //case 3
if (arr[mid]== arr[start] && arr[mid] == arr[end]){
if (arr[start] > arr[start+1]){
return start;
}
start++ ;
if (arr[end] < arr[end-1]){
return end-1;
}
end--;
}
else if (arr[start] <arr[mid] || arr[start]==arr[mid] && arr[end] < arr[mid] ){
start = mid+1;
}
else{
end = mid-1;
}
}
return -1 ;
}*/
public class Solution {
public int[] searchRange(int[] nums, int target) {
int[] result = new int[2];
result[0] = findFirst(nums, target);
result[1] = findLast(nums, target);
return result;
}
private int findFirst(int[] nums, int target){
int idx = -1;
int start = 0;
int end = nums.length - 1;
while(start <= end){
int mid = (start + end) / 2;
if(nums[mid] >= target){
end = mid - 1;
}else{
start = mid + 1;
}
if(nums[mid] == target) idx = mid;
}
return idx;
}
private int findLast(int[] nums, int target){
int idx = -1;
int start = 0;
int end = nums.length - 1;
while(start <= end){
int mid = (start + end) / 2;
if(nums[mid] <= target){
start = mid + 1;
}else{
end = mid - 1;
}
if(nums[mid] == target) idx = mid;
}
return idx;
}
}
}