Nth Digit
MEDIUMDescription
Given an integer n, return the nth digit of the infinite integer sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...].
Example 1:
Input: n = 3 Output: 3
Example 2:
Input: n = 11 Output: 0 Explanation: The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which is part of the number 10.
Constraints:
1 <= n <= 231 - 1
Approaches
Checkout 2 different approaches to solve Nth Digit. Click on different approaches to view the approach and algorithm in detail.
Brute-Force Simulation
This approach simulates the creation of the infinite integer sequence [1, 2, 3, ...] digit by digit. We iterate through numbers, and for each number, we check if the nth digit falls within its string representation.
Algorithm
- Initialize a counter
num = 1. - Start an infinite loop.
- Convert
numto its string representations. - Get the length of the string,
len. - If
nis less than or equal tolen, the target digit is within the current number. Return the digit at indexn-1ofs. - Otherwise, subtract
lenfromnand incrementnumto proceed to the next number.
The most intuitive way to solve this problem is to simulate the process directly. We can generate the sequence of numbers starting from 1 and keep track of the total number of digits seen so far.
The algorithm proceeds as follows:
- Start with the number
i = 1. - In a loop, convert the current number
ito its string representation. - Let the length of this string be
len. - If our target index
nis less than or equal tolen, it means the digit we are looking for is within the current numberi. The desired digit is the(n-1)th character of the string. - Otherwise, the digit is in a subsequent number. We subtract
lenfromnto move our target index forward and incrementito consider the next number. - This process continues until the
nth digit is found.
For example, if n = 11:
num=1,s="1",len=1.11 > 1.nbecomes11-1=10.num=2,s="2",len=1.10 > 1.nbecomes10-1=9. ...num=9,s="9",len=1.3 > 1.nbecomes2.num=10,s="10",len=2.2 <= 2. The digit is in "10". It's the(2-1)=1st character, which is '0'.
class Solution {
public int findNthDigit(int n) {
// This approach is too slow and will cause Time Limit Exceeded for large n.
int num = 1;
while (true) {
String s = Integer.toString(num);
int len = s.length();
if (n <= len) {
return Character.getNumericValue(s.charAt(n - 1));
}
n -= len;
num++;
}
}
}
Complexity Analysis
Pros and Cons
- Conceptually simple and easy to implement.
- Correct for small values of
n.
- Extremely inefficient for large values of
nas specified in the constraints. - Will not pass the time limits on most platforms.
Code Solutions
Checking out 5 solutions in different languages for Nth Digit. Click on different languages to view the code.
public class Solution {
public int FindNthDigit(int n) {
int k = 1, cnt = 9;
while ((long) k * cnt < n) {
n -= k * cnt;
++k;
cnt *= 10;
}
int num = (int) Math.Pow(10, k - 1) + (n - 1) / k;
int idx = (n - 1) % k;
return num.ToString()[idx] - '0';
}
}Video Solution
Watch the video walkthrough for Nth Digit
Similar Questions
5 related questions you might find useful
Algorithms:
Patterns:
Companies:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.