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

• Difficulty Level : Medium
• Last Updated : 13 Sep, 2021

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=2
Output: 6
Explanation: 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=2
Output: 1

Efficient 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:

1. Suppose intCount(X, K) represents the count of integers from 1 to X having the last K digits as equal.
2. 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.
3. 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++

 `// C++ Program of the above approach`   `#include ` `using` `namespace` `std;`   `// Function to return the count of` `// integers from 1 to X having the` `// last K digits as equal` `int` `intCount(``int` `X, ``int` `K)` `{` `    ``// Stores the total count of integers` `    ``int` `ans = 0;`   `    ``// Loop to iterate over all` `    ``// possible values of z` `    ``for` `(``int` `z = 0; z < ``pow``(10, K);` `         ``z += (``pow``(10, K) - 1) / 9) {`   `        ``// Terminate the loop when z > X` `        ``if` `(z > X)` `            ``break``;`   `        ``// Add count of integers with` `        ``// last K digits equal to z` `        ``ans += ((X - z) / ``pow``(10, K) + 1);` `    ``}`   `    ``// Return count` `    ``return` `ans;` `}`   `// Function to return the count of` `// integers from L to R having the` `// last K digits as equal` `int` `intCountInRange(``int` `L, ``int` `R, ``int` `K)` `{` `    ``return` `(intCount(R, K)` `            ``- intCount(L - 1, K));` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `L = 49;` `    ``int` `R = 101;` `    ``int` `K = 2;`   `    ``// Function Call` `    ``cout << intCountInRange(L, R, K);`   `    ``return` `0;` `}`

## Java

 `// Java Program of the above approach` `import` `java.util.*;`   `class` `GFG{`   `// Function to return the count of` `// integers from 1 to X having the` `// last K digits as equal` `static` `int` `intCount(``int` `X, ``int` `K)` `{` `    ``// Stores the total count of integers` `    ``int` `ans = ``0``;`   `    ``// Loop to iterate over all` `    ``// possible values of z` `    ``for` `(``int` `z = ``0``; z < Math.pow(``10``, K);` `         ``z += (Math.pow(``10``, K) - ``1``) / ``9``) {`   `        ``// Terminate the loop when z > X` `        ``if` `(z > X)` `            ``break``;`   `        ``// Add count of integers with` `        ``// last K digits equal to z` `        ``ans += ((X - z) / Math.pow(``10``, K) + ``1``);` `    ``}`   `    ``// Return count` `    ``return` `ans;` `}`   `// Function to return the count of` `// integers from L to R having the` `// last K digits as equal` `static` `int` `intCountInRange(``int` `L, ``int` `R, ``int` `K)` `{` `    ``return` `(intCount(R, K)` `            ``- intCount(L - ``1``, K));` `}`   `// Driver Code` `public` `static` `void` `main(String[] args)` `{` `    ``int` `L = ``49``;` `    ``int` `R = ``101``;` `    ``int` `K = ``2``;`   `    ``// Function Call` `    ``System.out.print(intCountInRange(L, R, K));`   `}` `}`   `// This code is contributed by 29AjayKumar`

## Python3

 `# Python3 program for the above approach`   `# Function to return the count of` `# integers from 1 to X having the` `# last K digits as equal` `def` `intCount(X, K):` `    `  `    ``# Stores the total count of integers` `    ``ans ``=` `0`   `    ``# Loop to iterate over all` `    ``# possible values of z` `    ``for` `z ``in` `range``(``0``, ``int``(``pow``(``10``, K)), ` `                     ``int``((``pow``(``10``, K) ``-` `1``) ``/` `9``)):`   `        ``# Terminate the loop when z > X` `        ``if` `(z > X):` `            ``break`   `        ``# Add count of integers with` `        ``# last K digits equal to z` `        ``ans ``+``=` `int``((X ``-` `z) ``/` `int``(``pow``(``10``, K)) ``+` `1``)` `    `  `    ``# Return count` `    ``return` `ans`   `# Function to return the count of` `# integers from L to R having the` `# last K digits as equal` `def` `intCountInRange(L, R, K):` `    `  `    ``return``(intCount(R, K) ``-` `intCount(L ``-` `1``, K))`   `# Driver Code` `L ``=` `49` `R ``=` `101` `K ``=` `2`   `# Function Call` `print``(intCountInRange(L, R, K))`   `# This code is contributed by sanjoy_62`

## C#

 `// C# Program of the above approach` `using` `System;`   `class` `GFG{`   `// Function to return the count of` `// integers from 1 to X having the` `// last K digits as equal` `static` `int` `intCount(``int` `X, ``int` `K)` `{` `    ``// Stores the total count of integers` `    ``int` `ans = 0;`   `    ``// Loop to iterate over all` `    ``// possible values of z` `    ``for` `(``int` `z = 0; z < Math.Pow(10, K);` `         ``z += ((``int``)Math.Pow(10, K) - 1) / 9) {`   `        ``// Terminate the loop when z > X` `        ``if` `(z > X)` `            ``break``;`   `        ``// Add count of integers with` `        ``// last K digits equal to z` `        ``ans += ((X - z) / (``int``)Math.Pow(10, K) + 1);` `    ``}`   `    ``// Return count` `    ``return` `ans;` `}`   `// Function to return the count of` `// integers from L to R having the` `// last K digits as equal` `static` `int` `intCountInRange(``int` `L, ``int` `R, ``int` `K)` `{` `    ``return` `(intCount(R, K)` `            ``- intCount(L - 1, K));` `}`   `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` `    ``int` `L = 49;` `    ``int` `R = 101;` `    ``int` `K = 2;`   `    ``// Function Call` `    ``Console.Write(intCountInRange(L, R, K));`   `}` `}`   `// This code is contributed by shivanisinghss2110`

## Javascript

 ``

Output

`6`

Time Complexity: O(log K)
Space Complexity: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :