-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCeilingOfNumber.java
More file actions
34 lines (34 loc) · 870 Bytes
/
CeilingOfNumber.java
File metadata and controls
34 lines (34 loc) · 870 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
public class CeilingOfNumber {
public static void main(String[] args) {
//ceiling of the number is the smallest number >= the target number
int[] arr = {2, 3, 5, 9, 14 ,16 ,18};
int target = 17;
System.out.println(ceilingNumber(arr, target));
}
static int ceilingNumber(int[] arr, int target)
{
int start = 0;
int end = arr.length-1;
if(arr[arr.length-1]<target)
{
return -1;
}
while(start<=end)
{
int mid = start + (end - start)/2;
if(arr[mid]==target)
{
return arr[mid];
}
else if(arr[mid]>target)
{
end = mid-1;
}
else
{
start = mid+1;
}
}
return arr[start];
}
}