Find The K-th Lucky Number

Lucky numbers (digits in {6,8}) enumerated in order are binary numerals over the digit pair — convert k+1 to binary, map bits.

Medium
Time O(log k) Space O(log k)
LeetCode
3 min read
bit-manipulation math string

Problem#

A lucky number contains only the digits 6 and 8. Enumerated in ascending order (6, 8, 66, 68, 86, 88, 666, …), return the k-th one (1-indexed) as a string.

Examples#

  • k=1"6".
  • k=2"8"; k=4"68".

Constraints#

  • 1 <= k <= 10^9.

Approach#

There are 2^L lucky numbers of exactly L digits. Decode k by stripping leading-2^L blocks until it fits in the L-digit range, then write the remaining offset in binary mapping bit 0 → 6, 1 → 8.

The cleanest formulation: the n-th lucky number’s digits are the bits of n+1 (binary), excluding the leading 1, with 0 → 6, 1 → 8. So compute binary(k + 1), drop the high bit, map.

Solution#

Find The K-th Lucky Number
class Solution {
public:
string kthLuckyNumber(long long k) {
k += 1;
string bits;
while (k) { bits.push_back('0' + (k & 1)); k >>= 1; }
bits.pop_back(); // drop leading 1
reverse(bits.begin(), bits.end());
for (char& c : bits) c = (c == '0') ? '6' : '8';
return bits;
}
};

Editorial#

Lucky numbers form a complete binary tree where level L has 2^L nodes — exactly the numbers with L digits. BFS ordering then matches binary representation. Adding 1 places the index inside that ordering; dropping the leading bit gives the within-level offset.

Complexity#

  • Time: O(log k).
  • Space: O(log k) for the result string.

Concept revision#

Search ESC

Keyboard shortcuts

Shortcuts are disabled while typing in inputs.