Thursday, September 29, 2022

C++ Code A sorted array rotated find the the point from where it is rotated

  A sorted array rotated find the the point from where it is rotated


C ++ Code

#include <iostream>

using namespace std;


int find(int a[],int n)

{

     int low=0,up=n;

     int mid;

     while(low<=up)

     {

           mid=(up+low)/2;

           if(a[mid]<a[mid-1]&&a[mid]<a[mid+1])

              return a[mid];

           else if(a[mid]>a[low])

           low=mid+1;

           else

           up=mid-1;

    

     }

     return 0;

}

int main()

{

     int a[]={14,16,18,2,3,4,5,6};

     int num=find(a,7);

     printf("%d",num);

    

     return 0;

 }




Output :


2



No comments:

Post a Comment