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
Remove 9: Transform Sequence by Skipping Numbers with Digit 9Original:1234567891011...REMOVEResult:1234567810111st2nd3rd4th5th6th7th8th9th10th...Example: 8th number without digit 9 is 10Numbers 9, 19, 29, 39, 49, ... are all skipped
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
Asked in
Google 15 Amazon 10 Facebook 8
23.0K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen