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
anujtube2995 hours ago
simple python solution
 
class Solution:   def findFloor(self,A,N,X):       p=-1       for i in range(N):           if A[i]<=X:               p=A[i]           if A[i]>X:               break       if p!=-1:           return(A.index(p))       else:           return(p)
0
divakarkumar2000
This comment was deleted.
0
code_24x71 week ago
int findFloor(vector<long long> v, long long n, long long x){
        long long result = -1, mid, low = 0, high = n - 1; 
        while(low <= high) {
            mid = low + (high - low) / 2;
            if(v[mid] == x) return mid;
            else if(x > v[mid]) {
                low = mid + 1;
                result = mid;
            }
            else high = mid - 1;
        }
        return result;
    }
0
rohitjug19csPremium1 week ago
JAVA Solution :
static int findFloor(long arr[], int n, long x)
    {
      int ans=-1;
      int low = 0;
      int high = n-1;
      while(low <= high){
        int mid = (low + high)/2;
        
        if(arr[mid] == x)return mid;
        if(arr[mid] < x){
            ans = mid;
            low = mid + 1;
        }
        else high = mid - 1;
      }
      return ans;
    }
0
aditya_shekhar_1 week ago
int findFloor(vector<long long> arr, long long n, long long x){
        
        if (arr[0]>x) return -1;
        if (arr[n-1]<x) return n;
        
        int left=0, right=n-1, mid;
        
        while(left<=right){
            if (arr[mid]==x) return mid;
            else if (mid>0 && arr[mid]>x && arr[mid-1]<=x) return mid-1;//x is between mid and mid-1
            else if (arr[mid]>x) right=mid-1;
            else left=mid+1;
        }
        return -1;
    }
This code gives TLE error in the first test case, but I've tried doing this in O(logn) time complexity. Can anyone help with this?
 
Thanks in advance!
 
0
expresscode2 weeks ago
    long long  findFloor(vector<long long> v, long long n, long long x){

       auto i= lower_bound ( v.begin(), v.end(), x);
       
       if(*i==x)
        return i-v.begin();
        
        if(i==v.begin())
         return -1;
         
        return (i-v.begin()-1 );
        
    }
0
expresscode
This comment was deleted.
0
ajitvishwakarma3602 weeks ago
   int findFloor(vector<long long> v, long long n, long long x){
        
        int K = floor (x);
        
        long long s = 0;
        long long e = n-1;
        long long mx =-1;
        if (x < v[0])
        return -1;
        while( s<=e){
            long long mid = s+(e-s)/2;
            
            if (v[mid] <=K)
            {
                mx =max( mid, mx);
               s =mid +1;
            }
            else 
             e =mid -1;
        }
        // Your code here
        return mx;
    }
0
himanshuharsh19992 weeks ago
	long long low = 0, high = n-1, res = -1;
        
	if (x < v[low]) return -1;
    if (x >= v[high]) return high;
        
    while(low<=high){
    	long long mid = (low+high)/2;
        if (v[mid] == x)
        	return mid;
        else if (v[mid] > x)
        	high = mid-1;
        else{
            res = max(res,mid);
            low = mid+1;
            }
        }
        return res;
+1
vteradali52 weeks ago
class Solution{   static int findFloor(long arr[], int n, long x)   {       for(int i=n-1;i>0;i--){           if(arr[i]<x){               return i;                          }           else if(arr[i]<=x){               return i;           }       }       return -1;   }   }
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.