Count of integers in given range having their last K digits are equal
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:
- 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++
// 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>
Time Complexity: O(log K)
Space Complexity: O(1)
Similar Reads
Count of Integers in given range consisting only given set of Digits Given two integers L and R, and an array arr[] containing single digit integers, the task is to find all the integers in the range [L, R) consisting of digits from given array of digits. Examples: Input: L = 1, R = 100, arr[] = {2, 3, 5, 7}Output: 20Explanation: The number between 1 and 100 total in
5 min read
Count of numbers with all digits same in a given range Given two integers L and R denoting the starting and end values of a range, the task is to count all numbers in that range whose all digit are same, like 1, 22, 444, 3333, etc.Example: Input: L = 12, R = 68 Output: 5 Explanation: { 22, 33, 44, 55, 66} are the numbers with same digits in the given ra
9 min read
Count numbers that does not contain digit N in given range Given integers, N, L, and R, the task is to find the number of integers in the range L to R that does not contain the digit N. print the answer modulo 109 + 7. ( L ? R ? 101000000) Examples: Input: N = 5, L = 1, R = 10Output: 9Explanation: excluding all 5 others from 1 to 10 will be included in the
14 min read
Count of Numbers in a Range where digit d occurs exactly K times Given two positive integers L and R which represents a range and two more positive integers d and K. The task is to find the count of numbers in the range where digit d occurs exactly K times.Examples: Input: L = 11, R = 100, d = 2, k = 1 Output: 17 Required numbers are 12, 20, 21, 23, 24, 25, 26, 2
11 min read
Count of Numbers in a Range where digit d occurs exactly K times Given two positive integers L and R which represents a range and two more positive integers d and K. The task is to find the count of numbers in the range where digit d occurs exactly K times.Examples: Input: L = 11, R = 100, d = 2, k = 1 Output: 17 Required numbers are 12, 20, 21, 23, 24, 25, 26, 2
11 min read
Count of Numbers in a Range where digit d occurs exactly K times Given two positive integers L and R which represents a range and two more positive integers d and K. The task is to find the count of numbers in the range where digit d occurs exactly K times.Examples: Input: L = 11, R = 100, d = 2, k = 1 Output: 17 Required numbers are 12, 20, 21, 23, 24, 25, 26, 2
11 min read