The best programs are written so that computing machines can perform
them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist
who works with traditional aesthetic and literary forms as well as mathematical concepts, to
communicate the way that an algorithm works and to convince a reader that the results will be correct.
― Donald E. Knuth

Problem :- Find the minimum element in a sorted and rotated array.

Solution :-
We will use the principle of binary search [ Complexity :: O(log n) ].
See the implementation below.

#include<iostream>using namespace std;
// find the minimum element in rotated and sorted arrayint findMin(int arr[],int start,int end) {
if(start == end) {
return arr[start];
}
int mid = (start+end)/2;
if(arr[start] > arr[mid]) {
// min element lies in left subarrayreturn findMin(arr,start,mid);
}
else
if(arr[mid] > arr[end]) {
// min element lies in right subarrayreturn findMin(arr,mid+1,end);
}
else {
// array is sorted but not rotatedreturn arr[start];
}
}
// mainint main() {
int arr[] = { 3,4,5,6,7,1,2 };
//int arr[] = { 7,11,15,16};int size = sizeof(arr)/sizeof(arr[0]);
int min = findMin(arr,0,size-1);
cout<<"\nMinimum element :: "<<min;
cout<<endl;
return 0;
}

#include<iostream>using namespace std;
// find the minimum element in rotated and sorted arrayint findMin(int arr[],int start,int end) {
if(start == end) {
return arr[start];
}
int mid = (start+end)/2;
if(arr[start] > arr[mid]) {
// min element lies in left subarrayreturn findMin(arr,start,mid);
}
else
if(arr[mid] > arr[end]) {
// min element lies in right subarrayreturn findMin(arr,mid+1,end);
}
else {
// array is sorted but not rotatedreturn arr[start];
}
}
// mainint main() {
int arr[] = { 3,4,5,6,7,1,2 };
//int arr[] = { 7,11,15,16};int size = sizeof(arr)/sizeof(arr[0]);
int min = findMin(arr,0,size-1);
cout<<"\nMinimum element :: "<<min;
cout<<endl;
return 0;
}