edited by
569 views
5 votes
5 votes

Consider a sorted array of $k+1$ elements, where the elements are first $k$ natural numbers $-\left \{ 1, 2, 3, 4, 5,\dots, k \right \}$ and any one of those $k$ numbers is repeated. The time complexity of best algorithm to find that repeated number is:

  1. $O\left ( k \right )$
  2. $O\left ( k\log k \right )$
  3. $O\left ( \log k \right )$
  4. $O(1)$
edited by

1 Answer

Best answer
10 votes
10 votes

it can be done in O(logk)

i am writing small pseudo code with function 'algo'

algo(array,k)
{
  if(array[k/2] isduplicate) return k/2; /*isduplicate can be checked in O(1) */

  else if(array[k/2]==k/2 && notduplicate) return algo(right_half array,k/2);

  else if(array[k/2]!=k/2 && notduplicate) return algo(left_half array,k/2);

}

u can see at each iteration we are throwing away half array, so

T(k) = T(k/2) + O(1) 

T(k) = O(logk)

selected by
Answer:

Related questions

4 votes
4 votes
3 answers
2
3 votes
3 votes
1 answer
4
Bikram asked May 14, 2017
307 views
What is the worst case time complexity to calculate the depth of a directed acyclic graph (DAG) with ‘$V$’ vertices and ‘$E$’ edges?$O\left ( V+E \right )$$O\left...