-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestion6.java
38 lines (34 loc) · 963 Bytes
/
Question6.java
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
// Search in rotated sorted array
import java.util.*;
public class Question6{
public static void main(String args[]){
Scanner input= new Scanner(System.in);
int a[]= {4,5,6,7,8,9,10,1,2};
int target=input.nextInt();
int low=0;
int high=a.length-1;
while(low <= high){
int mid = (low+high)/2;
if(target==a[mid]){
System.out.println(target);
}
else if(a[mid]>=a[low]){
if(target>=a[low] && target<a[mid]){
high=mid-1;
}
else{
low=mid+1;
}
}
else if(a[mid]<=a[high]){
if(target>a[mid] && target<=a[high]){
low=mid+1;
}
else{
high=mid-1;
}
}
}
System.out.println(target);
}
}