-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathCeilingOfNum.java
More file actions
39 lines (34 loc) · 942 Bytes
/
CeilingOfNum.java
File metadata and controls
39 lines (34 loc) · 942 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
37
38
39
package com.company;
public class CeilingOfNum {
public static void main(String[] args) {
int[] arr = {2,3,5,9,14,16,18};
int target = 15;
int ans = Ceil(arr, target);
System.out.println(ans);
}
//return the smallest number in the array which is greater than or equal to the target
static int Ceil(int[] arr, int target){
int start = 0;
int end = arr.length - 1;
while(start <= end)
{
if(target> arr[arr.length-1]){
return -1;
}
int mid = start + (end-start)/2;
if(arr[mid] > target)
{
end = mid -1;
}
else if(arr[mid] < target)
{
start = mid + 1;
}
else
{
return mid;
}
}
return start;
}
}