-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrotatedArray.java
More file actions
36 lines (34 loc) · 768 Bytes
/
rotatedArray.java
File metadata and controls
36 lines (34 loc) · 768 Bytes
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
public class rotatedArray {
public static int search(int arr[], int bot, int top, int target){
while(bot <= top){
int m = (top + bot) / 2;
if( arr[m] == target ){
return m;
} else if (arr[m] > arr[m + 1]){
if(target < arr[0]){
bot = m + 1;
} else {
top = m - 1;
}
} else if (arr[m] < arr[m - 1]){
if(target < arr[0]){
bot = m + 1;
} else {
top = m - 1;
}
} else if(target > arr[m]) {
bot = m + 1;
} else {
top = m - 1;
}
}
return -1;
}
public static int search(int arr[], int target){
return search(arr, 0, arr.length - 1, target);
}
public static void main(String args[]){
int myArr[] = {14, 15, 17, 19, 22, 24, 4, 6, 7, 8, 9};
System.out.println(search(myArr, 14));
}
}