If all decimal numbers are concatenated in a string then we will get a string that looks like string P as shown below. We need to tell the Nth character of this string.
P = “12345678910111213141516171819202122232425262728293031….”
Examples:
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 breaking 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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
char getDigit( int N, int d)
{
string str;
stringstream ss;
ss << N;
ss >> str;
return str[d - 1];
}
char getNthChar( int N)
{
int sum = 0, nine = 9;
int dist = 0, len;
for (len = 1; ; len++)
{
sum += nine*len;
dist += nine;
if (sum >= N)
{
sum -= nine*len;
dist -= nine;
N -= sum;
break ;
}
nine *= 10;
}
int diff = ceil (( double )N / len);
int d = N % len;
if (d == 0)
d = len;
return getDigit(dist + diff, d);
}
int main()
{
int N = 251;
cout << getNthChar(N) << endl;
return 0;
}
|
Java
class GFG
{
static char getDigit( int N, int d)
{
String str=Integer.toString(N);
return str.charAt(d - 1 );
}
static char getNthChar( int N)
{
int sum = 0 , nine = 9 ;
int dist = 0 , len;
for (len = 1 ; ; len++)
{
sum += nine * len;
dist += nine;
if (sum >= N)
{
sum -= nine * len;
dist -= nine;
N -= sum;
break ;
}
nine *= 10 ;
}
int diff = ( int )(Math.ceil(( double )(N) / ( double )(len)));
int d = N % len;
if (d == 0 )
d = len;
return getDigit(dist + diff, d);
}
public static void main (String[] args)
{
int N = 251 ;
System.out.println(getNthChar(N));
}
}
|
Python3
def getDigit(N, d):
string = str (N)
return string[d - 1 ];
def getNthChar(N):
sum = 0
nine = 9
dist = 0
for len in range ( 1 ,N):
sum + = nine * len
dist + = nine
if ( sum > = N):
sum - = nine * len
dist - = nine
N - = sum
break
nine * = 10
diff = (N / / len ) + 1
d = N % len
if (d = = 0 ):
d = len
return getDigit(dist + diff, d);
N = 251
print (getNthChar(N))
|
C#
using System;
class GFG
{
static char getDigit( int N, int d)
{
string str = Convert.ToString(N);
return str[d - 1];
}
static char getNthChar( int N)
{
int sum = 0, nine = 9;
int dist = 0, len;
for (len = 1; ; len++)
{
sum += nine * len;
dist += nine;
if (sum >= N)
{
sum -= nine * len;
dist -= nine;
N -= sum;
break ;
}
nine *= 10;
}
int diff = ( int )(Math.Ceiling(( double )(N) /
( double )(len)));
int d = N % len;
if (d == 0)
d = len;
return getDigit(dist + diff, d);
}
static void Main()
{
int N = 251;
Console.WriteLine(getNthChar(N));
}
}
|
PHP
<?php
function getDigit( $N , $d )
{
$string = strval ( $N );
return $string [ $d - 1];
}
function getNthChar( $N )
{
$sum = 0;
$nine = 9;
$dist = 0;
for ( $len = 1; $len < $N ; $len ++)
{
$sum += $nine * $len ;
$dist += $nine ;
if ( $sum >= $N )
{
$sum -= $nine * $len ;
$dist -= $nine ;
$N -= $sum ;
break ;
}
$nine *= 10;
}
$diff = ( $N / $len ) + 1;
$d = $N % $len ;
if ( $d == 0)
$d = $len ;
return getDigit( $dist + $diff , $d );
}
$N = 251;
echo getNthChar( $N );
?>
|
Javascript
<script>
function getDigit(N, d)
{
let str = N.toString();
return str[d - 1];
}
function getNthChar(N)
{
let sum = 0, nine = 9;
let dist = 0, len;
for (len = 1; ; len++)
{
sum += nine * len;
dist += nine;
if (sum >= N)
{
sum -= nine * len;
dist -= nine;
N -= sum;
break ;
}
nine *= 10;
}
let diff = (Math.ceil((N) / (len)));
let d = N % len;
if (d == 0)
d = len;
return getDigit(dist + diff, d);
}
let N = 251;
document.write(getNthChar(N));
</script>
|
Time Complexity: O(Log N), where N is the given integer.
Auxiliary Space: O(1), since no extra Space used.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
27 Mar, 2023
Like Article
Save Article