Nth Digit - Complete Solution Guide
Nth Digit is LeetCode problem 400, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given an integer n , return the n th 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 11 th 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 <= 2 31 - 1
Detailed Explanation
The problem asks us to find the nth digit in the infinite sequence of integers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,... Given an integer 'n', we need to return the digit located at the nth position in this sequence. For example, if n=3, the answer is 3. If n=11, the answer is 0, because the 11th digit belongs to the number 10, and it's the '0'. The constraint is that 'n' can be a relatively large number (up to 2<sup>31</sup> - 1), so a naive approach of generating the entire sequence until the nth position would be inefficient.
Solution Approach
The solution first determines the number of digits of the number containing the nth digit. It iteratively subtracts the number of digits contributed by numbers with increasing lengths (1-digit, 2-digit, etc.) from 'n' until 'n' is small enough to fall within a particular range of numbers with the same number of digits. Once we know the digit length, we can determine the number containing the nth digit and then extract the specific digit at the corresponding index.
Step-by-Step Algorithm
- Step 1: Initialize `digits` to 1 (number of digits), `count` to 9 (number of integers with `digits` digits), and `start` to 1 (first integer with `digits` digits).
- Step 2: While `n` is greater than `digits * count`, subtract `digits * count` from `n`, increment `digits`, multiply `count` by 10, and multiply `start` by 10.
- Step 3: After the loop, `digits` represents the number of digits of the number containing the nth digit, `start` is the first number with `digits` digits, and `n` is the index of the digit within the range of numbers with the same number of digits.
- Step 4: Calculate the number that contains the nth digit by adding `(n - 1) // digits` to `start`. The integer division `//` is used.
- Step 5: Calculate the index of the nth digit within the number by taking the modulo `(n - 1) % digits`.
- Step 6: Convert the number to a string and extract the digit at the calculated index. In C, a different approach is taken to extract the digit without string conversion.
Key Insights
- Insight 1: We can group numbers by the number of digits they have (1-digit numbers, 2-digit numbers, 3-digit numbers, etc.). This allows us to efficiently determine which number contains the nth digit.
- Insight 2: We can calculate the number of digits in each group and subtract them from 'n' until 'n' falls within the range of a specific group. This simplifies finding the exact number and digit.
- Insight 3: Integer overflow should be carefully considered, especially when dealing with large 'n' values. Use `long` type in Java and C++ when necessary.
Complexity Analysis
Time Complexity: O(log(n))
Space Complexity: O(1)
Topics
This problem involves: Math, Binary Search.
Companies
Asked at: Accenture.