Infinite Sequence Nth Digit
Coding Problem Keys
Infinite Sequence Nth Digit
Problem Statement
The program must accept an integer value N and print the Nth digit in the integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 and so on till infinity.
Input Format
The first line contains the value of N.
Output Format
The first line contains one of the digits from 0 to 9.Constraints
1 <= N <= 10⁹Example Input/Output 1
Input
5Output
5
Explanation
The 5th digit in the sequence 1234567 is 5.
Example Input/Output 2
Input
11Output
0
Explanation
The 11th digit in the sequence 12345678910 is 0.Max Execution Time Limit: 400 millisecs
Solution
Programming Language: Python 3 Language
def funcDigit(n):
x, y, m = 1, 1, {}
while x <= 10:
m[x] = y
y = y + (10 ** x - 10 ** (x - 1)) * x
x += 1
for i, j in m.items():
if n < j:
break
i -= 1
y = m[i]
return str(10**(i - 1)+(n-y)//i)[(n - y) % i]
n=int(input())
print(funcDigit(n))
# Published By PKJCODERS
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments