Help rahul to search

wrong output

hello @nmaurya760 you are right that you have to apply binary search here but there is some trick in applying binary search here .
here you have to make comparison like if the element the starting element is smaller than the middle elemnet then you have to make other condition that if the element is greater then the starting and smaller then the mid and the opposite i.e if the element is greater then the mid and smaller then the last element .
here for your reference i am attaching the corrrect code by which you will be able to understand the implementation .
https://ide.codingblocks.com/s/338627
if you have any problem while understanding the code please ask here .
we will discuss then .
Happy Learning !!

I am trying to implement the same thing please check my code once


how can you expect your code to return valid answer when you are writing return -1 at final return statement .

All test cases are not passing with this code
5
4 5 1 2 3
2

hello @nmaurya760 this is the updated code .
there was just problem in initialising your array .


if you feel your doubt is cleared please mark this doubt as resolved .
Happy Learning !!

Why was that way wrong in which i had done earlier

And still it is giving wrong answer

@nmaurya760 the last code i shared is passing every test case.
please try to submit again.

Yes it is passing the testcases but for this test case
5
4 5 1 2 3
2
Output should be 3
But this code is giving 4

this code is giving 3 as ouput https://ide.codingblocks.com/s/338627
this is beacuse there is difference in the way you have coded.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.