in Algorithms retagged by
961 views
12 votes
12 votes

Given an unsorted array of $n$ distinct elements, you want to find this set of $\log n$ elements: those at positions $1,2,4,8,16, \ldots, n/2$ if array were sorted. In other words, find the largest element, the second largest element, the fourth largest element, the eighth largest element and so on, terminating with the median element.
Consider that we have an algorithm to find $k$th smallest in an array of size $n$ using $\theta(n)$ time. Assume $n$ is a power of $2.$ How fast can you find all these $\log n$ elements? (Hint: Similar to binary search, we never have to worry about one of the subarray)

  1. $\Theta(\log n)$
  2. $\Theta(n)$
  3. $\Theta(n \log n)$
  4. $\Theta\left(n^{2}\right)$
in Algorithms retagged by
961 views

2 Comments

amazing!
1
1

$ \large{\colorbox{yellow}{Detailed video solution of this question with direct time stamp}}$
All India Mock Test 2 - Solutions Part 2

0
0

2 Answers

14 votes
14 votes
We will first find $n/2$ largest element (or median) and then we can use the partition method from quick sort to put this median at the correct place.

This will take $\theta(n)$.

Now we will find  $n/4$ largest element. But here is a good news. You do not need to check all $n$ elements, you can just check the subarray left of the median. Which are $n/2$ elements only. Now again you need the median of these $n/2$ elements.

We have a recurrence here:

$T(n) = T(n/2) + \theta(n)$

We get $\theta(n)$ as the answer.
edited by
0 votes
0 votes
This algortihm is combo package of Binary search and Partitioning method.

4 Comments

This is almost like BS not exactly binary search in BS we need to find both sides after getting mid position here we need to only one of the parts of array .
0
0
Find n/2th element -> O(n) time + Partition around n/2th element -> O(n) time

Find n/4th element -> O(n/2) time + Partition around n /4th element -> O(n/2) time

and so on..

You have O(n) time overall, there is no binary search involved just because the input is being partition into half each time doesn't mean its binary search also note that the partitioned subarrays arent sorted either
0
0
I also said almost like ...I think u didn't interprets  languages
0
0