Nth character in Concatenated Decimal String
Last Updated :
27 Mar, 2023
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++
// C++ program to get Nth character in
// concatenated Decimal String
#include <bits/stdc++.h>
using namespace std;
// Utility method to get dth digit of number N
char getDigit(int N, int d)
{
string str;
stringstream ss;
ss << N;
ss >> str;
return str[d - 1];
}
// Method to return Nth character in concatenated
// decimal string
char getNthChar(int N)
{
// sum will store character escaped till now
int sum = 0, nine = 9;
// dist will store numbers escaped till now
int dist = 0, len;
// loop for number lengths
for (len = 1; ; len++)
{
// nine*len will be incremented characters
// and nine will be incremented numbers
sum += nine*len;
dist += nine;
if (sum >= N)
{
// restore variables to previous correct state
sum -= nine*len;
dist -= nine;
N -= sum;
break;
}
nine *= 10;
}
// get distance from last one digit less maximum
// number
int diff = ceil((double)N / len);
// d will store dth digit of current number
int d = N % len;
if (d == 0)
d = len;
// method will return dth numbered digit
// of (dist + diff) number
return getDigit(dist + diff, d);
}
// Driver code to test above methods
int main()
{
int N = 251;
cout << getNthChar(N) << endl;
return 0;
}
Java
// Java program to get Nth character in
// concatenated Decimal String
class GFG
{
// Utility method to get dth digit of number N
static char getDigit(int N, int d)
{
String str=Integer.toString(N);
return str.charAt(d - 1);
}
// Method to return Nth character in concatenated
// decimal string
static char getNthChar(int N)
{
// sum will store character escaped till now
int sum = 0, nine = 9;
// dist will store numbers escaped till now
int dist = 0, len;
// loop for number lengths
for (len = 1; ; len++)
{
// nine*len will be incremented characters
// and nine will be incremented numbers
sum += nine * len;
dist += nine;
if (sum >= N)
{
// restore variables to previous correct state
sum -= nine * len;
dist -= nine;
N -= sum;
break;
}
nine *= 10;
}
// get distance from last one digit
// less maximum number
int diff = (int)(Math.ceil((double)(N) / (double)(len)));
// d will store dth digit of current number
int d = N % len;
if (d == 0)
d = len;
// method will return dth numbered digit
// of (dist + diff) number
return getDigit(dist + diff, d);
}
// Driver code
public static void main (String[] args)
{
int N = 251;
System.out.println(getNthChar(N));
}
}
// This code is contributed by mits
Python3
# Python program to get Nth character in
# concatenated Decimal String
# Method to get dth digit of number N
def getDigit(N, d):
string = str(N)
return string[d-1];
# Method to return Nth character in concatenated
# decimal string
def getNthChar(N):
# sum will store character escaped till now
sum = 0
nine = 9
# dist will store numbers escaped till now
dist = 0
# loop for number lengths
for len in range(1,N):
# nine*len will be incremented characters
# and nine will be incremented numbers
sum += nine*len
dist += nine
if (sum >= N):
# restore variables to previous correct state
sum -= nine*len
dist -= nine
N -= sum
break
nine *= 10
# get distance from last one digit less maximum
# number
diff = (N // len) + 1
# d will store dth digit of current number
d = N % len
if (d == 0):
d = len
# method will return dth numbered digit
# of (dist + diff) number
return getDigit(dist + diff, d);
# Driver code to test above methods
N = 251
print (getNthChar(N))
# Contributed by Afzal_Saan
C#
// C# program to get Nth character in
// concatenated Decimal String
using System;
class GFG
{
// Utility method to get dth digit of number N
static char getDigit(int N, int d)
{
string str = Convert.ToString(N);
return str[d - 1];
}
// Method to return Nth character in
// concatenated decimal string
static char getNthChar(int N)
{
// sum will store character
// escaped till now
int sum = 0, nine = 9;
// dist will store numbers
// escaped till now
int dist = 0, len;
// loop for number lengths
for (len = 1; ; len++)
{
// nine*len will be incremented characters
// and nine will be incremented numbers
sum += nine * len;
dist += nine;
if (sum >= N)
{
// restore variables to previous
// correct state
sum -= nine * len;
dist -= nine;
N -= sum;
break;
}
nine *= 10;
}
// get distance from last one digit
// less maximum number
int diff = (int)(Math.Ceiling((double)(N) /
(double)(len)));
// d will store dth digit of
// current number
int d = N % len;
if (d == 0)
d = len;
// method will return dth numbered
// digit of (dist + diff) number
return getDigit(dist + diff, d);
}
// Driver code
static void Main()
{
int N = 251;
Console.WriteLine(getNthChar(N));
}
}
// This code is contributed by mits
PHP
<?php
// PHP program to get Nth character
// in concatenated Decimal String
// Method to get dth digit
// of number N
function getDigit($N, $d)
{
$string = strval($N);
return $string[$d - 1];
}
// Method to return Nth character
// in concatenated decimal string
function getNthChar($N)
{
// sum will store character
// escaped till now
$sum = 0;
$nine = 9;
// dist will store numbers
// escaped till now
$dist = 0;
// loop for number lengths
for($len = 1; $len < $N; $len++)
{
// nine*len will be incremented characters
// and nine will be incremented numbers
$sum += $nine * $len;
$dist += $nine;
if ($sum >= $N)
{
// restore variables to
// previous correct state
$sum -= $nine * $len;
$dist -= $nine;
$N -= $sum;
break;
}
$nine *= 10;
}
// get distance from last one
// digit less maximum number
$diff = ($N / $len) + 1;
// d will store dth digit
// of current number
$d = $N % $len;
if ($d == 0)
$d = $len;
// method will return dth numbered
// digit of (dist + diff) number
return getDigit($dist + $diff, $d);
}
// Driver code
$N = 251;
echo getNthChar($N);
// This code is contributed by mits
?>
JavaScript
<script>
// JavaScript program to get Nth
// character in concatenated
// Decimal String
// Utility method to get dth
// digit of number N
function getDigit(N, d)
{
let str = N.toString();
return str[d - 1];
}
// Method to return Nth character in
// concatenated decimal string
function getNthChar(N)
{
// Sum will store character
// escaped till now
let sum = 0, nine = 9;
// dist will store numbers
// escaped till now
let dist = 0, len;
// Loop for number lengths
for(len = 1; ; len++)
{
// nine*len will be incremented
// characters and nine will be
// incremented numbers
sum += nine * len;
dist += nine;
if (sum >= N)
{
// Restore variables to
// previous correct state
sum -= nine * len;
dist -= nine;
N -= sum;
break;
}
nine *= 10;
}
// Get distance from last one digit
// less maximum number
let diff = (Math.ceil((N) / (len)));
// d will store dth digit
// of current number
let d = N % len;
if (d == 0)
d = len;
// Method will return dth numbered digit
// of (dist + diff) number
return getDigit(dist + diff, d);
}
// Driver Code
let N = 251;
document.write(getNthChar(N));
// This code is contributed by code_hunt
</script>
Time Complexity: O(Log N), where N is the given integer.
Auxiliary Space: O(1), since no extra Space used.
Similar Reads
N-th character in the string made by concatenating natural numbers Given an integer N, the task is to find the N-th character in the string made by concatenating natural numbers (Integers beginning from 1). The starting string will be "12345678910111213..". Examples: Input: N = 3 Output: 3 3rd character in the string "12345678910111213.." is 3. Input: N = 11 Output
11 min read
Decode the String of special characters Given an input string S of special characters where "@" represents "01", "#@" represents "111" and "##" represents "00". Decode the input string of special characters and print the number formed by decoding it. Examples: Input: S = "@#@##" Output: 60Explaination: @ represents "01" followed by #@ whi
9 min read
Convert all substrings of length 'k' from base 'b' to decimal A string defining a valid number is given. Output all the base conversions of substrings of length 'k' from base 'b' to base 10. Examples: Input : str = "12212", k = 3, b = 3. Output : 17 25 23 Explanation : All the substrings of length 'k' are : 122, 221, 212. Base conversion can be computed using
10 min read
Convert from any base to decimal and vice versa Given a number and its base, convert it to decimal. The base of number can be anything such that all digits can be represented using 0 to 9 and A to Z. The value of A is 10, the value of B is 11 and so on. Write a function to convert the number to decimal. Examples: Input number is given as string a
15+ min read
BCD or Binary Coded Decimal Binary Coded Decimal (BCD) is a binary encoding system in which each decimal digit is represented by a fixed number of binary bits, typically four. Instead of converting the entire decimal number into a binary number, BCD represents each decimal digit separately as its binary equivalent. BCD powers
6 min read