Open In App

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

Last Updated : 13 Sep, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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 <bits/stdc++.h>
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
<script>

// JavaScript program for the above approach

// Function to return the count of
// integers from 1 to X having the
// last K digits as equal
function intCount(X, K)
{
    
    // Stores the total count of integers
    let ans = 0;

    // Loop to iterate over all
    // possible values of z
    for(let z = 0; z < Math.pow(10, K);
            z += Math.floor((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 += Math.floor(((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
function intCountInRange(L, R, K)
{
    return(intCount(R, K) - 
           intCount(L - 1, K));
}

// Driver Code
let L = 49;
let R = 101;
let K = 2;

// Function Call
document.write(intCountInRange(L, R, K));

// This code is contributed by Potta Lokesh

</script>

Output
6

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


Similar Reads