I am getting wrong output trying to find out the pivot element index in rotated sorted array
Rotated sorted array pivot
@coderajay03 when any conditions are not satisfying your pivot will not update in your code and hence it will contain garbage value.use this to find pivot index
void solution(vector &A, int B) {
int low=0;
int high=A.size()-1;
int n= high+1;
int pivot;
while(low<=high){
int mid=(low+high)/2;
if (mid < high && A[mid] > A[mid + 1])
{
pivot = mid;
break;
}
if (mid > low && A[mid] < A[mid - 1])
{
pivot = mid-1;
break;
}
if (A[low] >= A[mid])
high = mid-1;
else
low = mid+1;
}
cout<<pivot<<" size is "<<n<<endl;
}
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.