If all decimal numbers are concatenated in a string then we will get a string which looks like string P as shown below. We need to tell Nth character of this string.
P = “12345678910111213141516171819202122232425262728293031….”
N = 10 10th character is 1 N = 11 11th character is 0 N = 50 50th character is 3 N = 190 190th character is 1
We can solve this problem by break-ing the string length-wise. We know that in decimal 9 numbers are of length 1, 90 numbers are of length 2, 900 numbers are of length 3 and so on, so we can skip these numbers according to the given N and can get the desired character.
Processing for N = 190 is explained below, P[184..195] = “979899100101” First getting length of number at N, 190 – 9 = 181 number length is more than 1 181 – 90*2 = 1 number length is more than 2 1 – 900*3 < 0 number length is 3 Now getting actual character at N, 1 character after maximum 2 length number(99) is, 1 Processing for N = 251 is explained below, P[250..255] = “120121” First getting length of number at N, 251 - 9 = 242 number length is more than 1 242 – 90*2 = 62 number length is more than 2 62 – 900*3 < 0 number length is 3 Now getting actual character at N, 62 characters after maximum 2 length number(99) is, Ceil(62/3) = 21, 99 + 21 = 120 120 is the number at N, now getting actual digit, 62%3 = 2, 2nd digit of 120 is 2, so our answer will be 2 only.
Please see below code for better understanding,
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print Kth character in sorted concatenated substrings of a string
- Generate a string whose all K-size substrings can be concatenated to form the given string
- Concatenated string with uncommon characters of two strings
- Concatenated string with uncommon characters in Python
- Maximum Consecutive Zeroes in Concatenated Binary String
- Print the final string when minimum value strings get concatenated in every operation
- Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated
- Find the K-th minimum element from an array concatenated M times
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Count of pairs of Array elements which are divisible by K when concatenated
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Convert Decimal To Hexa-Decimal including negative numbers
- Python | Replacing Nth occurrence of multiple characters in a String with the given character
- Find the Nth occurrence of a character in the given String
- Queries for Nth smallest character for a given range in a string
- Kth character from the Nth string obtained by the given operations
- Find the Nth digit from right in base B of the given number in Decimal base
- Find a string such that every character is lexicographically greater than its immediate next character
- Replace every character of string by character whose ASCII value is K times more than it
Improved By : Mithun Kumar