edited by
163 views
1 votes
1 votes
Let $A$ be a sorted array containing $n$ distinct integers, such that, for all $1 \leq i<j \leq n$, we have $A[i]<A[j]$. Note that the integers stored in the array $A$ are not necessarily from the set $\{1, \ldots, n\}$. Design an algorithm that outputs an index $i \in\{1, \ldots, n\}$ such that $A[i]=i$, if such an $i$ exists, and outputs $-1$ otherwise. The worst case running time of the algorithm should be asymptotically better than $O(n)$. Prove the correctness of your algorithm and state its asymptotic time complexity.
edited by

1 Answer

0 votes
0 votes

I think basic binary search should work ,
If A[i] is greater than "i" then we will move to left otherwise mid index would be the answer because anyway we are not going to have any case where element is less than the index it belongs as the array is distinct and sorted.
Time complexity : log(size_of_array)

Related questions

0 votes
0 votes
0 answers
2
1 votes
1 votes
1 answer
3
admin asked Aug 8, 2022
409 views
Consider a stack machine where the only available workspace is a stack whose elements are unsigned integers. We will denote the configuration of the stack by a sequence. ...