Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

// ROTATION COUNT IN A ROTATED AND SORTED ARRAY

#include <iostream>
using namespace std;

int main() {
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int beg,end,mid,index=-1;
beg=0;
end=n-1;
mid=(beg+end)/2;
while(beg<=end){
if(arr[mid]>arr[mid+1]){
index=mid;
break;
}
else if(arr[mid]<arr[mid-1]){
index=mid-1;
break;
}
if(arr[mid]>arr[end]){
beg=mid+1;
mid=(beg+end)/2;
}
else if(arr[mid]<arr[end]){
end=mid-1;
mid=(beg+end)/2;
}
}
int count=0;
count=end-index;
cout<<count<<endl;
}
return 0;
}

You might also like