Remove 9 - Problem
Start from integer 1, remove any integer that contains the digit 9 such as 9, 19, 29, etc. Now, you will have a new integer sequence [1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 21, ...].
Given an integer n, return the nth (1-indexed) integer in the new sequence.
Note: The sequence is 1-indexed, meaning the first element is at position 1.
Input & Output
Example 1 — Basic Case
$
Input:
n = 8
›
Output:
10
💡 Note:
The sequence without 9s is [1,2,3,4,5,6,7,8,10,11,...]. The 8th number is 10 (we skip 9).
Example 2 — Small Position
$
Input:
n = 5
›
Output:
6
💡 Note:
The sequence is [1,2,3,4,5,6,7,8,10,...]. The 5th number is 6.
Example 3 — After Multiple 9s
$
Input:
n = 18
›
Output:
20
💡 Note:
Sequence: [1,2,3,4,5,6,7,8,10,11,12,13,14,15,16,17,18,20,...]. 18th number is 20 (skip 9,19).
Constraints
- 1 ≤ n ≤ 8 × 108
Visualization
Tap to expand
Understanding the Visualization
1
Original Sequence
Normal counting: 1,2,3,4,5,6,7,8,9,10,11,...
2
Remove 9s
Skip any number containing digit 9
3
New Sequence
Result: 1,2,3,4,5,6,7,8,10,11,...
Key Takeaway
🎯 Key Insight: The sequence without 9s is equivalent to base-9 counting mapped to digits 1-8
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code