Problems > Order by Set Bits > Editorial

Order by Set Bits — Solution & Editorial

Back to the Problem

Sort with the composite key: (popcount(x), x). Stability/tie-breaks come from including the value itself in the key.

Complexity: O(N log N · key cost)