Nth Digit
To solve this coding challenge, we need to take the
th digit of the concatenated sequence of positive integers [1, 2, 3, ..., 10, 11, ...] and return that specific digit. The challenge requires an understanding of how numbers grow in size as they are concatenated and how to locate the
th digit in this continually growing series.
n
n
Explanation
Problem Breakdown
-
Initial Cases:
For small values of
directly less than 10, the result is simply
nitself.n - Dissect Number Lengths:
- Single-digit numbers contribute directly to the sequence.
- Starting from two-digit numbers, entries significantly grow. For example, once we pass 9, we get "10," "11," "12," and so on.
- Increment Through Lengths:
- Determine how many digits are covered by numbers with a varying number of digits (e.g., 1-9, 10-99, 100-999).
-
Adjust
To A Base : Subtract 1 from
nto convert it into a zero-indexed position for easier calculations.n - Identify Number Length :
-
Identify the range by increasing the length of numbers and keep decrementing
by the product of the current length and the count of such length numbers till
nremains within a valid range.n - Determine Exact Number :
-
Determine the exact number that contains the
th digit.
n -
Find the exact index of the
th digit in that number.
n -
Input:
n = 3 -
Output:
3
Explanation:
-
The sequence is
; third digit is obviously
1, 2, 3, ....3
Example 2:
-
Input:
n = 11 -
Output:
0
Explanation:
-
The sequence is
. The 11th digit is
1, 2, ..., 9, 10, 11, ...from0.10 -
Small
Case : Direct return.
n -
While Loop
: Decreases
until it fits within the digit count of a specific length.
n - Calculate Exact Number : Using zero-based index and division, then using modulus to locate digit within this number.
Steps to Solve
Detailed Steps in Pseudocode
# Step-by-step Explanation
function findNthDigit(n):
# If n is less than 10, it is a single-digit number
if n < 10:
return n
# Convert n to zero-based to facilitate easier calculation
n = n - 1
# Initialize lengths and counts
length = 1 # Represents the current length of numbers we are checking
count = 9 # Numbers 1-9 have 9 digits total
# Loop to determine the length range of the digit's position
while n > length * count:
n = n - (length * count) # Decrease n by the total digit count in this length range
length = length + 1 # Increase the length (1 digit numbers to 2 digit, etc)
count = count * 10 # 9, 90, 900... (numbers in current length range)
# Determine the actual number containing the nth digit
# The initial number of length 'length' in current range
start_number = 10 ** (length - 1)
# Locate the exact number containing the n-th digit
# '/' gives the number's relative position
number_containing_nth_digit = start_number + (n // length)
# Determine the exact digit within number_containing_nth_digit
# '%' gives the digit's index
digit_index_in_number = n % length
# Convert number_containing_nth_digit to string to access the specific digit and convert back to integer
result_digit = int(str(number_containing_nth_digit)[digit_index_in_number])
return result_digit