Nth Digit - Problem
Imagine an infinite sequence of positive integers written without any separators: 123456789101112131415.... Your task is to find the nth digit in this sequence.
Given an integer n, return the nth digit of the infinite integer sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, ...].
Key insights:
- Single-digit numbers (1-9) contribute 9 digits
- Two-digit numbers (10-99) contribute 90 ร 2 = 180 digits
- Three-digit numbers (100-999) contribute 900 ร 3 = 2700 digits
- And so on...
The challenge is to efficiently determine which number contains the nth digit and which position within that number.
Input & Output
example_1.py โ Basic Case
$
Input:
n = 3
โบ
Output:
3
๐ก Note:
The sequence is 123456789101112... The 3rd digit is 3.
example_2.py โ Two-Digit Range
$
Input:
n = 11
โบ
Output:
0
๐ก Note:
The sequence is 123456789101112... Positions 1-9 are single digits (123456789), position 10 is '1' from '10', position 11 is '0' from '10'.
example_3.py โ Edge Case
$
Input:
n = 15
โบ
Output:
2
๐ก Note:
The sequence is 123456789101112131415... Position 15 falls on the second digit of number 12, which is '2'.
Constraints
- 1 โค n โค 231 - 1
- n is guaranteed to be valid
- The sequence starts from 1 (not 0)
Visualization
Tap to expand
Understanding the Visualization
1
Pattern Recognition
1-digit numbers (1-9) contribute 9 digits total
2
Range Expansion
2-digit numbers (10-99) contribute 90ร2=180 digits
3
Mathematical Jump
Skip entire ranges to find target number quickly
4
Precise Extraction
Calculate exact position within the target number
Key Takeaway
๐ฏ Key Insight: Instead of counting digit by digit, use mathematical patterns to calculate which number contains the nth digit and extract it directly in O(log n) time.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code