-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathCeilingOfNumber.java
30 lines (29 loc) · 944 Bytes
/
CeilingOfNumber.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
package ArrayProblemsLeetcode.GoodQuestions;
//ceiling of number means finding a smallest number which is just greater or equal to the target number
public class CeilingOfNumber {
public static void main(String[] args) {
//array should be sorted first -> applying binary search for best optimization
int [] arr={1,4,5,9,13,17,20,23,30};
int target= 31;
int ans=ceilingOfNum(arr,target);
System.out.println(ans);
}
private static int ceilingOfNum(int[] arr, int target) {
int start=0;
int end=arr.length-1;
if(target>arr[end]){
return -1;
}
while(start<=end){
int mid=start+(end-start)/2;
if(target>arr[mid]){
start=mid+1;
}else if(target<arr[mid]){
end=mid-1;
}else {
return arr[mid];
}
}
return arr[start] ;
}
}