# Count of integers in given range having their last K digits are equal

Given a range from L to R and an integer K, the task is to count the number of integers in the given range such that their last K digits are equal.Example: Input: L = 49, R = 101, K=2Output: 6Explanation: There are 6 possible integers t.e., 55, 66, 77, 88, 99 and 100 such that their last K(i.e., 2) digits are equal.Input: L = 10, R = 20, K=2Output: 1Efficient Approach: It can be observed that the count of integers i in the range 1 to X having the last  K digits equal to an integer z (i.e., i % 10K = z) are (X – z)/10K + 1. Using this observation the above problem can be solved using the below steps:Suppose intCount(X, K) represents the count of integers from 1 to X having the last K digits as equal.To calculate intCount(X, K), iterate over all possibilities of z having K digits (i.e., {00…0, 11…1, 22…2, 33…3, 44…4, …., 99…9 }) in the formula (X – z)/10K +1 and maintain their sum which is the required value.Therefore, the count of integers in range L to R  can be obtained as intCount(R, K) – intCount(L-1, K).Below is the implementation of the above approach:C++  #include using namespace std;  int intCount(int X, int K){        int ans = 0;              for (int z = 0; z < pow(10, K);         z += (pow(10, K) - 1) / 9) {                  if (z > X)            break;                          ans += ((X – z) / pow(10, K) + 1);    }          return ans;}  int intCountInRange(int L, int R, int K){    return (intCount(R, K)            – intCount(L – 1, K));}  int main(){    int L = 49;    int R = 101;    int K = 2;          cout