question–>
https://practice.geeksforgeeks.org/problems/minimum-element-in-a-sorted-and-rotated-array/0
code–>
it is not passing all test cases help!!!
question–>
https://practice.geeksforgeeks.org/problems/minimum-element-in-a-sorted-and-rotated-array/0
code–>
it is not passing all test cases help!!!
See there was issue in this line
if(arr[mid]<arr[mid-1] and mid>s){
return arr[mid-1];//updated
}
if(arr[mid]<arr[mid-1]){
return arr[mid];//updated
}
But you will fail test case for this
5
4 5 1 2 3
Try to do it recursively
int fun(int arr[], int s, int e)
{
// This condition is needed to handle the case when array is not rotated at all
if (e < s) return arr[0];
// If there is only one element left
if (e == s) return arr[s];
// Find mid
int mid = (e + s)/2;
if (mid < e && arr[mid + 1] < arr[mid])
return arr[mid + 1];
if (mid > s && arr[mid] < arr[mid - 1])
return arr[mid];
if (arr[e] > arr[mid])
return fun(arr, s, mid - 1);
return fun(arr, mid + 1, e);
}
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.