Find The K-th Lucky Number - Problem
Find the K-th Lucky Number
In a mystical world of numbers, 4 and 7 are considered lucky digits. A number is called lucky if it contains only these lucky digits (4 and 7).
Your task is to find the k-th lucky number in ascending order. For example:
- 1st lucky number:
"4" - 2nd lucky number:
"7" - 3rd lucky number:
"44" - 4th lucky number:
"47" - 5th lucky number:
"74" - 6th lucky number:
"77"
Goal: Given an integer k, return the k-th lucky number as a string.
Input & Output
example_1.py โ Python
$
Input:
k = 1
โบ
Output:
"4"
๐ก Note:
The 1st lucky number is "4" (the smallest number containing only digits 4 and 7)
example_2.py โ Python
$
Input:
k = 5
โบ
Output:
"74"
๐ก Note:
Lucky numbers in order: 1st="4", 2nd="7", 3rd="44", 4th="47", 5th="74"
example_3.py โ Python
$
Input:
k = 10
โบ
Output:
"474"
๐ก Note:
The 10th lucky number falls in the 3-digit level: "444", "447", "474", "477", "744", "747", "774", "777" where "474" is the 10th overall
Constraints
- 1 โค k โค 109
- The answer will fit in a 32-bit integer when converted to number
- Lucky numbers contain only digits 4 and 7
Visualization
Tap to expand
Understanding the Visualization
1
Tree Structure
Level 1 has 2 numbers (4,7), Level 2 has 4 numbers (44,47,74,77), etc.
2
Level Detection
Calculate which level contains the k-th number using powers of 2
3
Position Mapping
Convert the position within level to binary representation
4
Binary to Lucky
Map each binary bit: 0โ'4', 1โ'7' to construct the result
Key Takeaway
๐ฏ Key Insight: Lucky numbers form a complete binary tree where each level doubles the count. Using binary representation allows direct computation in O(log k) time.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code