tutorialcup
Find Minimum In Rotated Sorted Array
Problem Statement
"Find Minimum In Rotated Sorted Array" states that you are given a sorted array of size n which is rotated at some index. Find the minimum element in the array.
Example
a = {5, 1, 2, 3, 4}
1
Explanation: If we arrange the array in sorted order it will be . So the smallest number out of all these is 1. And that's why the output is 1.
a = {5, 2, 3, 4}
2
Linear Search
Algorithm
1. Initialize a sorted rotated array a of size n.
2. Create a function to find the minimum in a rotated sorted array which accepts an integer variable as it's a parameter.
3. Initialize an integer variable min as the first element in the given array.
4. Traverse through the given array and check if the element at current index in array a is less than variable min, update min as current element.
5. Return the integer variable min.
We can use linear search or simply traverse the input array once and find the smallest element in a single traversal. We will simply loop over all the elements in the array and if we find a number which is lesser than our minimum, then we update our answer.
Code
C++ Program to find minimum in rotated sorted array
#include
using namespace std;
int findMin(int a, int n){
int min = a;
for(int i=1; i
www.tutorialcup.com/interview/array/find-minimum-in-rotat...
Find Minimum In Rotated Sorted Array
Problem Statement
"Find Minimum In Rotated Sorted Array" states that you are given a sorted array of size n which is rotated at some index. Find the minimum element in the array.
Example
a = {5, 1, 2, 3, 4}
1
Explanation: If we arrange the array in sorted order it will be . So the smallest number out of all these is 1. And that's why the output is 1.
a = {5, 2, 3, 4}
2
Linear Search
Algorithm
1. Initialize a sorted rotated array a of size n.
2. Create a function to find the minimum in a rotated sorted array which accepts an integer variable as it's a parameter.
3. Initialize an integer variable min as the first element in the given array.
4. Traverse through the given array and check if the element at current index in array a is less than variable min, update min as current element.
5. Return the integer variable min.
We can use linear search or simply traverse the input array once and find the smallest element in a single traversal. We will simply loop over all the elements in the array and if we find a number which is lesser than our minimum, then we update our answer.
Code
C++ Program to find minimum in rotated sorted array
#include
using namespace std;
int findMin(int a, int n){
int min = a;
for(int i=1; i
www.tutorialcup.com/interview/array/find-minimum-in-rotat...