Count of integers in given range having their last K digits are equal | Function to return the count of integers from 1 to X having the last K digits as equal ; Stores the total count of integers ; Loop to iterate over all possible values of z ; Terminate the loop when z > X ; Add count of integers with last K digits equal to z ; Return count ; Function to return the count of integers from L to R having the last K digits as equal ; Driver Code ; Function Call
function intCount ( X , K ) { let ans = 0 ; for ( let z = 0 ; z < Math . pow ( 10 , K ) ; z += Math . floor ( ( Math . pow ( 10 , K ) - 1 ) / 9 ) ) { if ( z > X ) break ; ans += Math . floor ( ( ( X - z ) / Math . pow ( 10 , K ) + 1 ) ) ; } return ans ; } function intCountInRange ( L , R , K ) { return ( intCount ( R , K ) - intCount ( L - 1 , K ) ) ; } let L = 49 ; let R = 101 ; let K = 2 ; document . write ( intCountInRange ( L , R , K ) ) ;