Count of integers in given range having their last K digits are equal | Java Program of the above approach ; 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
import java . util . * ; class GFG { static int intCount ( int X , int K ) { int ans = 0 ; for ( int z = 0 ; z < Math . pow ( 10 , K ) ; z += ( Math . pow ( 10 , K ) - 1 ) / 9 ) { if ( z > X ) break ; ans += ( ( X - z ) / Math . pow ( 10 , K ) + 1 ) ; } return ans ; } static int intCountInRange ( int L , int R , int K ) { return ( intCount ( R , K ) - intCount ( L - 1 , K ) ) ; } public static void main ( String [ ] args ) { int L = 49 ; int R = 101 ; int K = 2 ; System . out . print ( intCountInRange ( L , R , K ) ) ; } }