Ceil The Floor | Practice | GeeksforGeeks

Given an unsorted array Arr[] of N integers and an integer X, find floor and ceiling of X in Arr[0..N-1].
Floor of X is the largest element which is smaller than or equal to X. Floor of X doesn’t exist if X is smaller than smallest element of Arr[].
Ceil of X is the smallest element which is greater than or equal to X. Ceil of X doesn’t exist if X is greater than greatest element of Arr[].
Example 1:
Input:
N = 8, X = 7
Arr[] = {5, 6, 8, 9, 6, 5, 5, 6}
Output: 6 8
Explanation:
Floor of 7 is 6 and ceil of 7 
is 8.

Example 2:
Input:
N = 8, X = 10
Arr[] = {5, 6, 8, 9, 6, 5, 5, 6}
Output: 9 -1
Explanation:
Floor of 10 is 9 but ceil of 10 is not 
possible.

Your Task:
You don't need to read input or print anything. Your task is to complete the function getFloorAndCeil() which takes the array of integers arr, n and x as parameters and returns a pair of integers denoting the answer. Return -1 if floor or ceil is not present.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints :
1 ≤ N ≤ 105
1 ≤ Arr[i], X ≤ 106
 
0
codewithshoaib191 month ago
Pair getFloorAndCeil(int[] arr, int n, int x) {       // code here       Arrays.sort(arr);       int a=ceilOfANumber(arr,x);       int b=floorOfANumber(arr,x);       Pair p=new Pair(a,b);       return p;   }   public static int ceilOfANumber(int[] arr, int x) {       int s = 0;       int n = arr.length;       int e = n - 1;       int res = -1;       while (s <= e) {           int mid = s + (e - s) / 2;           if (arr[mid] == x) {               return arr[mid];           } else if (arr[mid] < x) {               res = arr[mid];               s = mid + 1;           } else if (arr[mid] > x) {               e = mid - 1;           }       }       return res;   }   public static int floorOfANumber(int[] arr, int target) {       int s = 0;       int n = arr.length;       int e = n - 1;       int res = -1;       while (s <= e) {           int mid = s + (e - s) / 2;           if (arr[mid] == target) {               return arr[mid];           } else if (arr[mid] < target) {               s = mid + 1;           } else if (arr[mid] > target) {               res = arr[mid];               e = mid - 1;           }       }       return res;   }
0
akash15100132 months ago
#User function Template for python3def getFloorAndCeil(arr, n, x):   floor = -1   ceil = max(arr)   F=False   C=False   i = 0   while(i<=n-1):       if arr[i] <= x and arr[i] >= floor:               floor = arr[i]               F=True              if arr[i] >= x and arr[i] <= ceil:               ceil = arr[i]               C=True       i = i+1   if C == False:       ceil = -1   if F == False:       floor = -1          return (floor,ceil)
0
palshivanshpal2 months ago
Pair getFloorAndCeil(int[] arr, int n, int x) {       Arrays.sort(arr);       int f=-1,c=-1;       for(int i=0;i<n;i++){           if(arr[i]<=x)           f=arr[i];           if(arr[i]>=x){               c=arr[i];               break;           }       }       Pair p1=new Pair(f,c);       return p1;   }
0
imabhishek022 months ago
Easy solution with bad time complexity:
pair<int, int> getFloorAndCeil(int arr[], int n, int x) {
   int start=0;
   int end=n-1;
   int res=-1;
   
   while(start<=end)
   {
       int mid=start +(end-start)/2;
       if(arr[mid]==x)
       {
           res= arr[mid];
       }
       else if(arr[mid]<x)
       {
           res=arr[mid];
           start=mid+1;
       }else
       {
           end=mid-1;
       }
   }
   int s=0;
   int e=n-1;
   int r=-1;
   
   while(s<=e)
   {
       int m=s +(e-s)/2;
       if(arr[m]==x)
       {
           r= arr[m];
       }
       else if(arr[m]<x)
       {
           
           s=m+1;
       }else
       {
           r=arr[m];
           e=m-1;
       }
   }
   return {res,r};
}
0
hemantkrkrayla6 months ago
C++ solution
pair<int, int> getFloorAndCeil(int arr[], int n, int x) {
    // code here
    int ceil_of_x = 1e9;
    int floor_of_x = -1e9;
    for(int i=0;i<n;i++) {
        if(arr[i] >= x) {
            ceil_of_x = min(ceil_of_x,arr[i]);
        }
        if(arr[i] <= x) {
            floor_of_x = max(floor_of_x,arr[i]);
        }
    }
    if(ceil_of_x==1e9) {
        ceil_of_x = -1;
    }
    if(floor_of_x==-1e9) {
        floor_of_x = -1;
    }
    return make_pair(floor_of_x,ceil_of_x);
    
}
+1
badgujarsachin836 months ago
pair<int, int> getFloorAndCeil(int arr[], int n, int x) {
    // code here
    int f=-1;
    int c=100000;
    for(int i=0;i<n;i++){
        if(arr[i]<=x){
            f=max(arr[i],f);
        }
        if(arr[i]>=x){
            c=min(arr[i],c);
        }
    }
    if(c==100000){
        c=-1;
    }
    return {f,c};
}
+1
gaurlakshya24267 months ago
c++ solution
pair<int, int> getFloorAndCeil(int arr[], int n, int x) {
    int flor=-1;
    int ceel=INT_MAX;
    for(int i=0;i<n;i++){
        if(arr[i]<=x){
            flor=max(flor,arr[i]);
        }
        if(arr[i]>=x){
            ceel=min(ceel,arr[i]);
        }
    }
    if(ceel==INT_MAX) ceel=-1;
    
    return {flor,ceel}; 
}
0
Kartik Verma10 months ago
Kartik Verma
https://uploads.disquscdn.c...
0
Sanjit Paul1 year ago
Sanjit Paul
Very easy Java solutionhttps://ide.geeksforgeeks.o...
-1
Rohit Yadav1 year ago
Rohit Yadav
def getFloorAndCeil(arr, n, x):    # code here    mn=[-1]    mx=[]    for i in range (n):        if  x >= arr[i]:            mn.append(arr[i])        if x<= arr[i]:            mx.append(arr[i])    if  len(mx)==0:        return max(mn),'-1'    else:        return max(mn) ,min(mx)
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.