fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. // element serch in rotated element in n times
  7. int rot;
  8. cout<<"Enter the number of rotation\n";
  9. cin>>rot;
  10. int arr[]={4,5,6,7,8,9,10};
  11. int size=7;
  12. int ans=-1;
  13. for(int i=0;i<rot;i++){
  14. int temp=arr[size-1];
  15. for(int j=size-1;j>0;j--){
  16. arr[j]=arr[j-1];
  17. }
  18. arr[0]=temp;
  19. }
  20.  
  21. int target=6;
  22. cout<<"search element in rotated element \n";
  23. int start=0,end=size-1;
  24. while(start<=end){
  25. int mid=start+(end-start)/2;
  26. if(arr[mid]==target){
  27. cout<<"TARGET PRSENT : INDEX :"<<mid<<" TARGETED VALUE :"<< arr[mid];
  28. return 0;
  29. }else if(arr[0]<=arr[mid]){
  30. if(arr[0]<=target&&arr[mid]>target){
  31. end=mid-1;
  32. }else{
  33. start=mid+1;
  34. }
  35. }else{
  36. if(arr[mid]<target&&arr[end]>target){
  37. start=mid+1;
  38. }else{
  39. end=mid-1;
  40. }
  41. }
  42. }
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0.01s 5288KB
stdin
4
stdout
Enter the number of rotation
search element in rotated element