Problems > Order by Digit Sum > Editorial

Order by Digit Sum — Solution & Editorial

Back to the Problem

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

Complexity: O(N log N · key cost)