Skip to main content

Remove 9 - Solution & Explanation

HardPremiumFree on FleetCodeMath3 min readAsked at: Houzz
Practice this problem

Problem Statement

Start from integer 1, remove any integer that contains 9 such as 9, 19, 29...

Now, you will have a new integer sequence [1, 2, 3, 4, 5, 6, 7, 8, 10, 11, ...].

Given an integer n, return the nth (1-indexed) integer in the new sequence.

 

Example 1:

Input: n = 9
Output: 10

Example 2:

Input: n = 10
Output: 11

 

Constraints:

  • 1 <= n <= 8 * 108

Approach Overview

Problem Overview: You are given an integer n. Return the nth positive integer that does not contain the digit 9. Instead of generating all numbers and filtering, the key observation is that numbers without digit 9 behave like a base-9 numbering system.

Approach 1: Brute Force Enumeration (O(n log n) time, O(1) space)

The straightforward solution is to iterate through positive integers and count how many do not contain the digit 9. For each number, repeatedly extract digits using modulo and division to check if any digit equals 9. If the number is valid, increment the counter until it reaches n. This works but becomes slow as n grows because many numbers must be checked and discarded. Each validation requires scanning digits, giving roughly O(log x) work per number where x is the current candidate. This approach mainly demonstrates the problem constraints but rarely passes strict performance expectations.

Approach 2: Base-9 Conversion (O(log n) time, O(1) space)

The optimal solution relies on a mathematical insight. If digit 9 is forbidden, every valid number only uses digits 0–8. That means the sequence of valid numbers behaves exactly like numbers written in base 9. Instead of skipping numbers containing 9, convert n directly into base 9. The digits of this base-9 representation form the answer when interpreted as a normal decimal number. Implementation is simple: repeatedly divide n by 9, append n % 9 as the next digit, and build the result. This avoids checking any invalid numbers and jumps directly to the correct value.

This trick works because counting without the digit 9 reduces the digit choices from 10 to 9. The mapping between sequence index and value becomes identical to base-9 numbering. Problems like this commonly appear in math and base conversion patterns where digit constraints change the effective number system.

Recommended for interviews: The base-9 conversion approach is what interviewers expect. Mentioning the brute force approach first shows you understand the requirement, but recognizing the digit restriction as a base conversion problem demonstrates stronger algorithmic insight. This pattern often appears in math and number theory style questions where counting rules alter the number system.

Solutions for this problem are being prepared.

Try solving it yourself

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force EnumerationO(n log n)O(1)Useful for understanding the problem or when constraints are very small
Base-9 Conversion (Optimal)O(log n)O(1)Best approach for large n; directly maps the index to a valid number

Video Solution

660. Remove 9 (Leetcode Hard)Programming Live with Larry407 views views

Watch 1 more video solutions →

Frequently Asked Questions

Is Remove 9 easy or hard?
Remove 9 is labeled Hard because the trick is recognizing the base-9 relationship. The implementation itself is short, but the insight that skipping digit 9 turns the sequence into base-9 numbering is not immediately obvious.
Remove 9 Python/Java solution
Most implementations convert n into base 9 using repeated division and remainder operations. The same logic works in Python, Java, C++, and Go. Each iteration extracts a digit with n % 9 and reduces n using integer division by 9.
How to solve Remove 9 in O(log n)?
Use base-9 conversion. Repeatedly divide n by 9 and append the remainder (n % 9) as the next digit of the answer. Continue until n becomes zero. The resulting digits form the number that corresponds to the nth positive integer without digit 9.
What is the best approach for Remove 9?
The best approach converts the given number n into base 9. Since valid numbers cannot contain digit 9, the allowed digits are 0–8, which corresponds exactly to a base-9 numbering system. Converting n to base 9 directly produces the nth number without digit 9. This runs in O(log n) time and O(1) space.
Is Remove 9 asked at Google/Amazon/Meta?
Remove 9 represents a classic math and number-system transformation problem. Variants of digit-restriction or base-conversion questions have appeared in interviews at companies like Google and Amazon because they test mathematical insight rather than brute-force coding.
What data structure is used in Remove 9?
No complex data structure is required. The optimal solution relies purely on arithmetic operations and base conversion logic. Some implementations temporarily store digits while building the result, but the algorithm mainly uses math operations.
What is the time complexity of Remove 9?
The optimal base-9 conversion solution runs in O(log n) time because each step divides the number by 9. Only a few iterations equal to the number of digits in base 9 are required. Space complexity remains O(1) since the result is built using simple arithmetic operations.

Ready to solve this problem?

Practice Remove 9 with our built-in code editor and test cases.

Practice on FleetCode