Given an array the size of n where: 1/2 of the array is with a single (unknown) value. 1/4 of the array is with a single (unknown) different value. And so on for 1/8, 1/16, 1/32 Give an algorithm to sort the array. You cannot use the find median algorithm
So what I figured is: There are only logn different values There is a simple solution using a binary heap on O ( n*loglogn) It looks like a question that needed to be solved in O (n)
Here is one possible approach:
The whole algorithm thus runs in amortized O(n) time, i.e., it is dominated by eliminating duplicates and expanding the sorted array. The space complexity is O(n).
This is essentially optimal because you need to "touch" all the elements to print the sorted array, which means we have a matching lower bound of Omega(n) on the running time.
The idea is to use the Majority algorithm that takes O(n) then discovering what's the "half" value deleting it from the array and then doing it again on the new array n+n/2+n/4+n/8+..... < 2n => O (n)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With