Math, asked by Rohankumar4502, 1 year ago

Given a sorted array, arr[] and a value, x, find floor of x in given array. Floor of x is the largest element in arr[] such that the element is smaller than or equal to x. If floor exists, then return index of it, else return -1

Answers

Answered by paulaiskander2
2

int floorSearch(int arr[], int x)  

{  

   n = arr.length;

   // If last element is smaller than x  

   if (x >= arr[n-1])  

       return n-1;  

 

   // If first element is greater than x  

   if (x < arr[0])  

       return -1;  

 

   // search for the first element  greater than x

   for (int i=1; i<n; i++)  

   if (arr[i] > x)  

       return (i-1);  

 

   return -1;  

}  

Similar questions