w3resource

C Exercises: Find minimum element in a sorted and rotated array

C Array: Exercise-57 with Solution

Write a program in C to find the minimum element in a sorted and rotated array.

Pictorial Presentation:

C Exercises: Find minimum element in a sorted and rotated array.

Sample Solution:

C Code:

#include <stdio.h>

int findMin(int arr1[],int start,int end) 
{
   if(start == end) 
   {
      return arr1[start];
   }
   int mid = (start+end)/2;
   if(arr1[start] > arr1[mid]) 
   {
      return findMin(arr1,start,mid);
   }
   else
   if(arr1[mid] > arr1[end]) 
   {
      return findMin(arr1,mid+1,end);
   }
   else 
   {
      return arr1[start];
   }
}

int main() 
{
   int arr1[] = { 3,4,5,6,7,9,2 };
   int n = sizeof(arr1)/sizeof(arr1[0]);
   int i;
    //------------- print original array ------------------	
	printf("The given array is :  ");
	for(i = 0; i < n; i++)
	{
	printf("%d  ", arr1[i]);
    } 
	printf("\n");
//------------------------------------------------------ 
   int min = findMin(arr1,0,n-1);
   printf("The minimum element in the above array is: %d ",min);
   return 0;
}

Sample Output:

The given array is :  3  4  5  6  7  9  2  
The minimum element in the above array is: 2 

Flowchart:

Flowchart: Find minimum element in a sorted and rotated array.

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a program in C to return the minimum number of jumps to reach the end of the array.
Next: Write a program in C to move all zeroes to the end of a given array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

C Programming - What is the argument for printf that formats a long?

Put an l (lowercased letter L) directly before the specifier.

unsigned long n;
long m;

printf("%lu %ld", n, m);

Ref : https://bit.ly/3dIwfkP