0

I am working with very interesting [problem][1]:

Given an unsorted array of n distinct elements, We want to find this set of logn elements: those at positions 1,2,4,8,16,..., n/2 in the sorted order. (The element at position 1 in the sorted order is the minimum, the one in position 2 is the 2nd smallest, ..., and the one in position n is the largest.) Assume n is a power of 2 . How fast can you find all these logn elements?

My thought process: we can find elements of any rank from an array in O(n) using the median of medians. I will not find elements in the order written above which is 1,2,4,8,16,..., n/2. I will first take the middle of asked elements (say k) which is at index (logn)/2 and then i will divide original unsorted array into two parts using two parts using k and i will work on these two parts indiviually. Using this approach i am getting loglogn levels and at each level n amount of work is to be done hence time complexity is nloglogn.

But the options given link does not have this option.

[1](page 2) https://d1b10bmlvqabco.cloudfront.net/attach/ixj45csz3f961q/grs52ylqx9y/j28yy7bo8jvm/final.pdf

user3699192
  • 25
  • 1
  • 9
  • And what is your question exactly? – rici Jun 12 '22 at 01:48
  • I want to confirm my approach since none of the options is matching. I have also seen similar question before at https://gateoverflow.in/25865/Tifr-cse-2013-part-b-question-18. – user3699192 Jun 12 '22 at 02:01
  • "then i will divide original unsorted array into two parts using two parts using k and i will work on these two parts indiviually". Why? Only one part is relevant. – rici Jun 12 '22 at 02:27
  • suppose i am looking for 3 elements which are 1st smallest, 10th smallest, and 20th smallest. Now if i first find 10th smallest and put it in the right position (just like pivot in quicksort) then left array is relevant to me to find 1st smallest and right array is relevant to me to find 20th smallest hence both parts are relevant. – user3699192 Jun 12 '22 at 02:53
  • But the problem clearly states "those at positions 1,2,4,8,16,..., n/2 in the sorted order". So you find n/2; everything above that is irrelevant. Then you find n/4. The only relevant thing above that is n/2, but you already found that. Then you find n/8. Etc. At no point do you need to work on two sides of the partition. The result is O(n), because eash quickselect works on n/2**i elements, for i from 0 to (log2 n)-1, and that sum is 2n. – rici Jun 12 '22 at 04:00
  • I think I haven't got question yet. Thanks for your willingness to help. you have also answered some of my previous doubts in the compiler. Can you confirm if Question is asking to sort logn elements at positions 1,2,4,8,16,..., n/2 ? – user3699192 Jun 12 '22 at 05:24
  • It's asking you to find the elements that would be at positions 1, 2, 4, 8,16,…, n/2 if the array were sorted. Or in other words, to find the largest element, the second largest element, the fourth largest element, the eighth largest element and so on, terminating with the median element. – rici Jun 12 '22 at 07:45
  • You could just sort the array, and read off the values in those positions. But sorting takes O(n log n). Given some index k, Quickselect partitions the array at the element which would be at index element `k` in the sorted array. After the partition, all smaller elements are at the beginning and all larger ones are at the end. It takes O(N). – rici Jun 12 '22 at 07:55
  • Awesome. Got it. Thanks By the way there is a variant and very different question which has been asked in one of the toughest CS exam https://gateoverflow.in/25865/Tifr-cse-2013-part-b-question-18 – user3699192 Jun 12 '22 at 08:16
  • Yes, this is a special case where you can take advantage of the particular nature of the pivot points. It's not the only special case. – rici Jun 12 '22 at 14:33

0 Answers0