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 * 108Imagine we are writing down a list of numbers in order, but we completely skip any number that contains the digit nine. The brute force approach simply inspects every single whole number starting from one, checks if it contains a nine, and keeps a tally of the valid ones until we reach our target position.
Here's how the algorithm would work step-by-step:
def remove_nine_brute_force(target_position):
valid_numbers_count = 0
current_number = 0
# Loop infinitely because we must check consecutive numbers until reaching the target count
while True:
current_number += 1
# Convert to string to easily check for the forbidden digit without math operations
if '9' not in str(current_number):
valid_numbers_count += 1
# Stop once the tally matches our target position to reveal the final number
if valid_numbers_count == target_position:
return current_number
Instead of checking every single number one by one to see if it contains a forbidden digit, the optimal approach translates the target position into a different counting system. By changing how we count, we can instantly jump to the correct answer without doing heavy searching.
Here's how the algorithm would work step-by-step:
def new_integer_after_removal(input_position):
# We convert the decimal index to base nine because skipping nines is identical to base nine counting.
converted_digits = []
current_number = input_position
while current_number > 0:
current_remainder = current_number % 9
converted_digits.append(str(current_remainder))
current_number = current_number // 9
# Reversing is necessary because modulo operations extract digits from least to most significant.
converted_digits.reverse()
base_nine_string = ''.join(converted_digits)
# Parsing as base 10 integer yields the exact value after skipping numbers containing nine.
return int(base_nine_string or '0')
| Case | How to Handle |
|---|---|
| Input n equals 1, the absolute minimum positive integer. | The base case correctly converts and returns 1 since it contains no 9. |
| Inputs containing the digit 9 such as 9, 19, or 90. | The conversion algorithm treats the number space as base 9 instead of base 10 to skip all 9-containing values. |
| Large inputs approaching maximum integer constraints like 2 to the power of 31 minus 1. | Using standard 32-bit or 64-bit integer division and modulo operations ensures efficient logarithmic time scaling without overflow. |
| Zero or negative inputs which fall outside the positive integer domain. | The problem statement specifies positive integers, so inputs start strictly at 1 and upward. |
| Boundary numbers transitioning across decades where digits shift like 8 to 10 or 89 to 100. | Repeated modulo 9 operations correctly handle base shifting to bypass the digit 9 seamlessly. |