AfterAcademy Tech
•
16 Jan 2020

Problem Description: You have a sorted and rotated array where elements are sorted and rotated circularly. Write a program to find the minimum element in the array.
Examples
Input: array[] = [4, 5, 6, 7, 1, 2, 3]
Output: 1
Input: array[] = [8, 9, 4, 5, 6, 7 ]
Output: 4
Input: array[] = [3, 4, 5, 6, 7]
Output: 3
Input: array[] = [3, 2]
Output: 2
We will be discussing two solutions to this problem:
The idea is to traverse the array from the first element to the last element of the array and maintain a minimum.
Solution steps
Pseudo Code
int findMin(int array[], int size)
{
int minimum = INT_MAX
for(int i=0 to size)
minimum = min(minimum, array[i])
return minimum
}
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Critical Ideas to Think
If we look closely then you will find that there are at most three cases that could be encountered.
if we have case 1 then we just return the leftmost value in the array. If we have case 2, we will try to find the minimum on the right side of the array. If we have case 3, we will try to find the minimum to the left side of the array.
Solution steps
Pseudo Code - Iterative
int findMin(int array[], int size)
{
int left = 0
int right = size - 1
while(left < right)
{
if(array[left] < array[right]) // case 1
return array[left]
int mid = left + (right-left)/2
if(array[mid] > array[right])
left = mid + 1 // case 2
else
right = mid // case 3
}
return array[left]
}
Pseudo Code - Recursive
// Helper function
int bsearch(int array[], int left, int right)
{
if(left < right)
{
if(array[left] < array[right]) // case 1
return array[left]
int mid = left + (right - left)/2
if(array[mid] > array[right])
return bsearch(array, mid + 1, right) // case 2
else
return bsearch(array, left, mid) // case 3
}
return array[left]
}
// Driver function
int findMin(int array[], int size)
{
return bsearch(array, 0, size - 1)
}
Complexity Analysis
The above approach looks similar to the idea of the binary search.
Time Complexity = O(log n)
Space Complexity = O(1)
Critical Ideas to Think

Please comment down below if you have alternative approaches or find an error/bug in the above approaches.
Happy Coding, Enjoy Algorithms!
AfterAcademy Tech
You are given two sorted arrays arr1[ ] and arr2[ ] of sizes m and n respectively. Write a program to merge them in such a way that the resultant array is sorted too.

AfterAcademy Tech
There are two sorted arrays nums1 and nums2 of size n. Find the median of the two sorted arrays.

AfterAcademy Tech
Given an array A[] of size n, you need to find the maximum and minimum element present in the array.Your algorithm should make minimum number of comparisons.

AfterAcademy Tech
Given a sorted array A[] with possibly duplicate elements, write a program to find indexes of first and last occurrences of an element k in the given array. This question will clear your concept of Binary Search
