Find Minimum in Rotated Sorted Array

In this tutorial, I am going to discuss a very interesting problem find minimum in rotated sorted array.

Given an array which is sorted in ascending order is rotated at some unknown point. Write a code to find the minimum element in a sorted and rotated array.

You may assume no duplicate exists in the array.

For example:

Example 1:

Input: [6, 7, 8, 1, 2]
Output: 1

The minimum element in this sorted and rotated array is 1.

Example 2:

Input: [8, 9, 10, 1, 0, 1, 2]
Output: 0

In this example, the minimum element is 0.

Find Minimum Element in a Sorted and Rotated Array

In this problem, the array is rotated between 1 to n times where n is the length of an array. How do we find the minimum in sorted rotated array.

In this tutorial, I am going to discuss multiple approaches and it’s java code to solve this problem.

Programming video tutorials

Find Minimum Element in a Sorted and Rotated Array using Linear Search

The simplest solution is to traverse complete array to find the minimum element.

Declare one variable and assign the element present at 0th index. Then, run a loop from 1 to n-1. Then, for each index value compare the current index value with the variable value. If current index value is less than the variable value then reassigned the minimum value.

The time complexity of this approach is O(n).

Find Minimum in Rotated Sorted Array using Binary Search

In our previous example, we have traversed complete array to solve this problem. How we can improve our solution?

In this example, I am going to explain how we can solve this problem using binary search in O(logn) time complexity.

If you are not familiar with the binary search algorithm then check my previous tutorials.

Binary search in java

Binary search using recursion in java

The array is sorted and rotated. So, It is clear that the minimum element in this array is the only element whose previous element is greater than it. If the array is sorted then the minimum element is the element present at 0th index.

In Binary Search, we first calculate the mid. To find the minimum element we can compare mid with mid-1 and mid+1 element. If it is not found at mid then the minimum element lies either in left or right half.

i) If the middle element is smaller than the last element, then the minimum element lies in the left half.
ii) Else minimum element lies in the right half.

Search in Rotated Sorted Array

In this video tutorial, I have discussed the multiple approaches to solve this problem.

Programming questions on Binary Search

Tagged , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.