Reputation: 114
I have any array where the elements are first in increasing order and then in a decreasing order
Like A[10]={1,4,6,8,3,2} , with no duplicates in an array.
if the input is 7, Output should be, Element do not exist.
Time complexity should b better thn O(n)
I am getting the result by the linear search, comparng each elemnts with the element to b found. But as I want solution better then O(n)
I tried by finding a pivot where the array element flips, and then searching from (0 to pivotelement) and then again searchiing in (last elemnt to pivot elemnt).
Please suggest. For O(n)
#include<iostream>
int main()
{
int A[10]={1,4,6,8,3,2};
int i,num
cout<<"Enter the element to be searched";
cin>>num
for(i=0;i<10;i++)
{
If(A[i]==num)
{
cout<<"Element exist";
break;
}
else
cout<<"Element do not exist";
}
}
Upvotes: 2
Views: 1474
Reputation: 14876
Algorithm:
How to do step 1 ?
EDIT
Just in case it was not clear: if you pick n/2-th element and turns out max is on the left, then you pick n/4-th element, if the max is on its right side, you only continue the search in the range n/4...n/2
elements.
Upvotes: 5