Found at last ques

https://ide.codingblocks.com/s/39568
my code for https://hack.codingblocks.com/contests/c/537/88
it is a simple recursive binary search to find last occurrence
But I am unable to understand why my code is getting stuck in an infinite loop.

Pls help

thanks

Hey Apoorva, you need to modify your if check for finding the last occurrence and the recursion calls must return something also to get back to the last call.
Update your if check as:

    if (( mid == e || search < a[mid+1]) && a[mid] == search) {
                   ans=mid;
    			return ans; 
               } 

I have updated your code you can refer this https://ide.codingblocks.com/s/39569.

alright, thank you for such a prompt response!

Hey, it is not passing 1/3 test cases and I feel that is so coz binary search works on a sorted array and if we feed an unsorted array, sort it and it apply binary search to find a number, it will return the index of the sorted array, which is different from the original array’s indexing.

Hey, This is not the question of binary search as the array we are getting is not sorted. you are supposed to use linear search only.

okay! I thought otherwise