-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCeilTheFloor.java
More file actions
33 lines (32 loc) · 850 Bytes
/
Copy pathCeilTheFloor.java
File metadata and controls
33 lines (32 loc) · 850 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
public class CeilTheFloor {
public static void main(String[] args) {
int[] arr= {5, 6, 8, 9, 6, 5, 5, 6};
int n=arr.length;
int x=7;
Pair ans=getFloorAndCeil(arr, n, x);
System.out.println(ans.floor+" "+ans.ceil);
}
public static Pair getFloorAndCeil(int[] arr, int n, int x) {
// code here
int floor=-1,ceil=Integer.MAX_VALUE;
for(int i=0;i<n;i++){
if(arr[i]>floor && arr[i]<=x){
floor=arr[i];
}
if(arr[i]<ceil && arr[i]>=x){
ceil=arr[i];
}
}
if(ceil==Integer.MAX_VALUE){
ceil=-1;
}
return new Pair(floor,ceil);
}
}
class Pair{
int floor,ceil;
Pair(int floor,int ceil){
this.floor=floor;
this.ceil=ceil;
}
}