Nth character in Concatenated Decimal String | Utility method to get dth digit of number N ; Method to return Nth character in concatenated decimal string ; Sum will store character escaped till now ; dist will store numbers escaped till now ; Loop for number lengths ; nine * len will be incremented characters and nine will be incremented numbers ; Restore variables to previous correct state ; Get distance from last one digit less maximum number ; d will store dth digit of current number ; Method will return dth numbered digit of ( dist + diff ) number ; Driver Code
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 ) ) ;