I dont know if i m wrong

#include
using namespace std;

int binarysearchrotated(int a[10000],int n,int key){
int s=0;
int e=n-1;

while(s<=e){
	int m=(s+e)/2;
	if(a[m]==key){
		return m;
	}
	else if(key<=a[m] and key>=a[s]){
		e=m-1;
	}
	else{
		s=m+1;
	}
}
return -1;

}

int main() {
int n;
cin>>n;

int arr[10000];
for(int i=0; i<n; i++){
	cin>>arr[i];
}

int key;
cin>>key;

cout<<binarysearchrotated(arr,n,key);
return 0;

}

by this code also we could search the element right ;; but why did prateek bhaiyya made 4 cases???

@thakur0_0 hey this code will not cover all test case as array is not fully sorted ,check this complete and corrected code:

#include using namespace std; int findPivot(int a[],int s,int e){ if(s>e){ return -1; } while(s<=e){ int mid = (s+e)/2; ///Current Element is Mid if(mid<e&&a[mid]>a[mid+1]){ return mid; } ///Prev of current element is mid if(mid>s&&a[mid]<a[mid-1]){ return mid-1; } if(a[s]>=a[mid]){ e = mid-1; } else{ s = mid + 1; } } return -1; } int main(){ int n; cin>>n; int a[1000]; for(int i=0;i<n;i++){ cin>>a[i]; } cout<<findPivot(a,0,n-1); // int a[] = {4,5,1,2,3}; // int b[] = {5,6,0,1,2,3}; // cout<<findPivot(b,0,5)<<endl; return 0; }

for input cases 4 5 1 2 3 it gies output as 1?

@thakur0_0 bhai tabhi apko sahi wala corrected code bheja hai ,isme kuch dikkt hai all test cases are not covered.See the corrected code above send by me.

the code you shared doesnt gives the correct output for array a[];

@thakur0_0 hey check this:


This code is correct and is being submitted correctly on hackerblocks

https://hack.codingblocks.com/app/practice/1/1044/problem

the code gives wrong answer for the following problem (Help Rahul from algorithm series)