// Binary Search : Time Complexity O(log n)
#include<bits/stdc++.h>
using namespace std;
int minimumElement(int a[], int low, int high)
{
while(low < high)
{
int mid = (low+high)/2;
if(a[mid] == a[high])
high--;
else if(a[mid] > a[high])
low = mid+1;
else
high = mid;
}
return a[high];
}
int main()
{
int n;
cout << "Enter Array Size :: ";
cin >> n;
int a[n];
cout << "\nEnter Array Element :: ";
for(int i=0;i<n;i++)
cin >> a[i];
int mn = minimumElement(a, 0, n-1);
cout << "\nMinimum Element is :: " << mn << "\n";
return 0;
}
Comments
Post a Comment