Quote:
Can anyone tell me why it is showing TIME LIMIT EXCEEDED?
You problem is that when your dichotomy loop encounter the searched value, the code falls in an infinite loop.
while(l <= h){
mid = (l+h)/2;
if( x < a[mid])
{
h = mid - 1;
}
else if(x > a[mid])
{
l = mid + 1;
}
else
{
first_index = mid;
last_index = mid;
}
}
And it is just the first problem in your code.