-
Notifications
You must be signed in to change notification settings - Fork 3
/
Bicycle.java
51 lines (46 loc) · 1.04 KB
/
Bicycle.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
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.io.*;
import java.util.*;
public class Bicycle
{
public static int bs(int arr[],int n)
{
int store = Integer.MAX_VALUE;
int start = 0;
int end = n-1;
while(start<=end)
{
int mid = start + (end-start)/2;
if(arr[mid]==mid && mid<store)
{
store = mid;
end = mid -1;
}
else if(arr[mid] < mid)
{
start = mid +1;
}
else if(arr[mid] > mid)
{
end = mid -1;
}
}
if(store == Integer.MAX_VALUE)
{
return -1;}
else
{
return store;
}
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int arr[] = new int[a];
for(int i=0;i<a;i++)
{
arr[i] = sc.nextInt();
}
System.out.println(bs(arr,a));
}
}