-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathceiling.java
60 lines (56 loc) · 1.33 KB
/
ceiling.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
52
53
54
55
56
57
58
59
60
package com.gitanjali;
public class ceiling {
public static void main(String[] args)
{
int[] arr={1,2,3,5,7,9,11,90};
System.out.println("ceilling " + ceililngBS(arr,997));
System.out.println("floor " + floorBS(arr,0));
}
static int ceililngBS(int[] num,int key) //smallest number greatest than key
{
if(key>num[num.length-1])
{
return -1;
}
int start=0;
int end=num.length-1;
while(start<=end)
{
int mid=start + (end-start)/2;
if(num[mid]==key){
return mid;
}
else if(key<num[mid]) {
end=mid-1;
}
else if(num[mid]<key)
{
start=mid+1;
}
}
return start;
}
//greatest no smaller than target;
static int floorBS(int[] arr,int key)
{
int start=0;
int end=arr.length-1;
while(start<=end)
{
int mid=start+(end-start)/2;
if(arr[mid]==key)
{
return mid;
}
else if(arr[mid]<key)
{
start=mid+1;
}
else if(arr[mid]>key)
{
end=mid-1;
}
}
return end;
}
}