- #1
evinda
Gold Member
MHB
- 3,836
- 0
Hello! (Nerd)
A sequence of numbers $A=(a_1, a_2, \dots, a_n)$ is called convex if there is a $k$ with $1 \leq k \leq n$ such that $\forall 1 \leq i \leq k-1$, we have that $a_i>a_{i+1}$ and $\forall k \leq i \leq n-1$ we have that $a_i<a_{i+1}$.
Write an algorithm with time complexity $O(\log n)$,that finds the minimum element of the sequence $A$.
That's what I have tried:
Could you tell me if it's right or if I have done something wrong? (Thinking)
A sequence of numbers $A=(a_1, a_2, \dots, a_n)$ is called convex if there is a $k$ with $1 \leq k \leq n$ such that $\forall 1 \leq i \leq k-1$, we have that $a_i>a_{i+1}$ and $\forall k \leq i \leq n-1$ we have that $a_i<a_{i+1}$.
Write an algorithm with time complexity $O(\log n)$,that finds the minimum element of the sequence $A$.
That's what I have tried:
Code:
Minimum(A[1...n], low, high){
int mid=low+floor((high-low)/2);
if (high<low) return -1;
if (A[mid]>A[mid-1] and mid-1>=low) return Minimum(A, low, mid-1);
else if (A[mid]<A[mid+1] and mid+1<=high) return Minimum(A,mid+1,high);
return A[mid];
Could you tell me if it's right or if I have done something wrong? (Thinking)