One test case is not passing in this question where i am wrong in this not able to understand.
Https://ide.codingblocks.com/s/469139
see for test case like
5
3
4
5
1
2
2
ans should be 4 not 0
Here is the Algorithm that will let you find the element in O(log n)
- Find middle point mid = (l + h)/2
- If the key is present at the middle point, return mid.
- Else If arr[l…mid] is sorted
a) If the key to be searched lies in the range from arr[l] to arr[mid], recur for arr[l…mid].
b) Else recur for arr[mid+1…r] - Else (arr[mid+1…r] must be sorted)
a) If the key to be searched lies in the range from arr[mid+1] to arr[r], recur for arr[mid+1…r].
b) Else recur for arr[l…mid]