forked from manishaparuthi/manisha_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminel.cpp
47 lines (41 loc) · 1.1 KB
/
minel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*Suppose a sorted array A is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
Find the minimum element.
The array will not contain duplicates.
NOTE 1: Also think about the case when there are duplicates. Does your current solution work? How does the time complexity change?*
PROBLEM APPROACH:
Note: If you know the number of times the array is rotated, then this problem becomes trivial. If the number of rotation is x, then minimum element is A[x].
Lets look at how we can calculate the number of times the array is rotated.
*/
using namespace std;
#include<iostream>
int main()
{
cout<<"Enter size of the array: ";
int size;
cin>>size;
int array[size];
cout<<"Enter Sorted Array: ";
for(int i=0;i<size;i++)
{
cin>>array[i];
}
//main logic.
int min=array[0];
int i=1;
int rot=0;
while(i<size)
{
if( (array[i]<array[i-1]) &&
(array[i]<array[i+1]) )
{
min=array[i];
rot=i;
break;
}
i++;
}
cout<<" array is rotated at: "<<rot;
cout<<endl<<"minimum element is: "<<min;
return 0;
}