Sorting binary numbers according to this order relation and finding k-th largest.

140 Views Asked by At

We have the following order relation on positive integers: let number A be less than number B in two cases:

• When the binary notation of A has fewer "ones" than B has.

• When the binary notation of A contains the same amount of "ones" that B has, then A is less than B when A is less than B in decimal notation

How would i quickly determine the k-th largest number in this order ?

I need tips im stuck .

1

There are 1 best solutions below

1
On

Hint. Observe that, since the binary representation of $2^n$ is $1$ followed by $n$ zeros, one has $2^0 < 2^1 < 2^2 < 2^3 < \cdots\ $ for your order. Then compare these numbers with numbers having at least two ones in their binary representation.