Max element in shifted array

Find max element in shifted array.

 23451 return 5

12345 return 5

C++:
01 int maxShifted(int A[], int size)
02 {
03     for(int i=0,j=size; ; ){
04         if(i+1==j)
05             return A[i]>A[j]?A[i]:A[j];
06         int mid=(i+j)/2;
07         if(A[i]<A[mid])
08             i=mid;
09         else
10             j=mid;
11     }
12 }
Advertisements
This entry was posted in Array and linked list, Binary search. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s