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
def intCount ( X , K ) : NEW_LINE INDENT ans = 0 NEW_LINE for z in range ( 0 , int ( pow ( 10 , K ) ) , int ( ( pow ( 10 , K ) - 1 ) / 9 ) ) : NEW_LINE INDENT if ( z > X ) : NEW_LINE INDENT break NEW_LINE DEDENT ans += int ( ( X - z ) / int ( pow ( 10 , K ) ) + 1 ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def intCountInRange ( L , R , K ) : NEW_LINE INDENT return ( intCount ( R , K ) - intCount ( L - 1 , K ) ) NEW_LINE DEDENT L = 49 NEW_LINE R = 101 NEW_LINE K = 2 NEW_LINE print ( intCountInRange ( L , R , K ) ) NEW_LINE