-
Notifications
You must be signed in to change notification settings - Fork 57
/
LowerIndexOfValue.java.txt
75 lines (59 loc) · 1.95 KB
/
LowerIndexOfValue.java.txt
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
package com.company;
public class LowerIndexOfValue {
public static void main(String[] args){
int[] a={2,3,4,5,3,1,1};
int target=1;
System.out.println(search(a,target));
}
static int search(int a[],int target){
int peak=mountainarray(a);
int start=0;
int firstTry=orderagnosticBs(a,target,start,peak);
if (firstTry!=-1){
return firstTry;
}
return orderagnosticBs(a,target,peak+1,a.length-1);
}
static int orderagnosticBs(int[] array,int target,int start,int end){
//find whether the array is sorted in ascending or des cending
boolean isASC = array[start]<array[end];
//OR
// if(array[start]<array[end])
// isASC=true;
// else
// isASC=false;
while(start<=end){
int middle=start+(end-start)/2; // we could have taken (start+end)/2 but the thing is it can
// exceed the integer range when we add start and end that why we use
if(array[middle]==target) // middle=start+(end-start)/2; formula
return middle;
if(isASC){
if(target>array[middle]){
start=middle+1;
}
if(target<array[middle]){
end=middle-1;
}
}else {
if(target>array[middle]){
end=middle-1;
}
if(target<array[middle]){
start=middle+1;
}
}
}
return -1;
}
static int mountainarray(int[] arr){
int start=0;
int end=arr.length-1;
while(start<end){
int mid = start+(end-start)/2;
if(arr[mid]>arr[mid+1])
end=mid;
else start=mid+1;
}
return start;
}
}