Floor in a Sorted Array | Practice | GeeksforGeeks

Given a sorted array arr[] of size N without duplicates, and given a value x. Floor of x is defined as the largest element K in arr[] such that K is smaller than or equal to x. Find the index of K(0-based indexing).
Example 1:
Input:
N = 7, x = 0 
arr[] = {1,2,8,10,11,12,19}
Output: -1
Explanation: No element less 
than 0 is found. So output 
is "-1".
Example 2:
Input:
N = 7, x = 5 
arr[] = {1,2,8,10,11,12,19}
Output: 1
Explanation: Largest Number less than 5 is
2 (i.e K = 2), whose index is 1(0-based 
indexing).

Your Task:
The task is to complete the function findFloor() which returns an integer denoting the index value of K or return -1 if there isn't any such number.
Expected Time Complexity: O(log N).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 107
1 ≤ arr[i] ≤ 1018
0 ≤ X ≤ arr[n-1]
0
harshilrpanchal19982 days ago
java solution
 
class Solution{      // Function to find floor of x   // arr: input array   // n is the size of array   static int findFloor(long arr[], int n, long x)   {      int count=0;for(int i=0;i<n;i++){  if(arr[i]<=x)  {   count =i;  }   else if(arr[0]>x)    return -1;}
return count;   }   } 
0
keshridivya51 week ago
//c++ solution using binary search
class Solution{
  public:
    // Function to find floor of x
    // n: size of vector
    // x: element whose floor is to find
    int findFloor(vector<long long> v, long long n, long long x){
        
       long long start = 0;
       long long end = n - 1;
       
       while(start <= end)
       {
           long long mid = start + (end - start) / 2;
           if(v[mid] == x) return mid;
           else if(v[mid] < x)
           {
               int res = mid;
               start = mid + 1;
           }
           else end = mid - 1;
       }
       return end; 
    }
};

0
tantade20022 weeks ago
static int findFloor(long arr[], int n, long k)   {       if(k<arr[0]) {  return -1; } if(k>arr[arr.length-1]) {  return arr.length-1; } int low=0; int high=arr.length-1; while(low<=high) {  int mid=(low+high)/2;  if(arr[mid]>k && arr[mid-1]<k) {   return mid-1;  }  if(arr[mid]>k && arr[mid-1]>k) {   high=mid-1;  }  if(arr[mid]<k && arr[mid+1]<k) {   low=mid+1;  }  if(arr[mid]<k && arr[mid+1]>k) {   return mid;  } } return -1;   }   }
+1
hiphopsamreen2 weeks ago
// > if the search ele is present then return it's index
// > else return the largest ele smaller than search ele
int findFloor(vector<long long> v, long long n, long long x)
{
    // Your code here
    int index = lower_bound(v.begin(), v.end(), x) - v.begin();
 
    if (index < n && v[index] == x)
        return index;
 
    else if (index - 1 >= 0)
        return --index;
 
    else
        return -1;
}
0
1ms19me0242 weeks ago
int findFloor(long long int arr[], int N, long long int K) {int count=0;for(int i=0;i<N;i++){   if(arr[i]<=K)   {    count++;   }    else if(arr[0]>K)     return -1;}int res=count-1;return res;}
0
shubham211019973 weeks ago
static int findFloor(long arr[], int n, long x)   {       int ans=-1;       int low=0,high=n-1;       while(low<=high){           int mid=(low+high)/2;           if(arr[mid]==x) return mid;           else if(arr[mid]>x)high=mid-1;           else{               ans=mid;               low=mid+1;           }                  }       return ans;   }
0
alokkumar90194 weeks ago
 int findFloor(vector<long long> v, long long n, long long x){
       
       // Your code here
       int a=v.size();
       int l=0, h=a-1,m;
       if(x>v[a-1]){
           return a-1;
       }
       while(l<=h){
           m=l+(h-l)/2;
           if(v[m]==x || (x>v[m] && x<v[m+1])){
               return m;
           }else if(v[m]==x || (x>v[m-1] && x<v[m])){
               return m-1;
           } 
           else if(v[m]>x){
               h=m-1;
           } else if(v[m]<x){
               l=m+1;
           }
       }
       return -1;
   }
0
vikasvasabathula1 month ago
def findFloor(self,A,N,X):
        if X==0:
            return -1
        else:
            for i in range(X,0,-1):
                if(i in A):
                    return A.index(i)
        return -1
-7
lakshitpant7741 month ago
class Solution{      // Function to find floor of x   // arr: input array   // n is the size of array   static int findFloor(long arr[], int n, long x)   {       for(int i=0;i<n;i++){           if(arr[i]<=x){               return i;       }   }   return -1;    }}
0
zunjarprasad08971 month ago
 static int findFloor(long arr[], int n, long x)   {       int low =0;        int high =n-1;       int res =-1;       while(low <= high){           int mid = (low+high)/2;                      if(arr[mid] == x){               return mid;           }           if(arr[mid] > x){               high = mid -1;           }else{               low = mid +1;               res = mid;           }       }       return res;   }
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.