Here is the link to the question (Question). And I will give my function’s code below.
I am using an iterative approach. But I am getting TLE when submitting. Please help in identifying the error. Even though the given example test cases are working.
int findFloor(vector<long long> a, long long n, long long x){
if(a[0]>x)
{
return -1;
}
else if(x>a[n-1])
{
return n-1;
}
else
{
int i=0,j=n-1;
while(i<=j)
{
int m=i+j / 2;
if(a[m]==x || (a[m]<x && a[m+1]>x))
{
return m;
}
else if(a[m]<x)
{
i=m+1;
}
else
{
j=m-1;
}
}
}
// Your code here
}