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++
#include <bits/stdc++.h>
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 << intCountInRange(L, R, K);
return 0;
}
|
Java
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));
}
}
|
Python3
def intCount(X, K):
ans = 0
for z in range ( 0 , int ( pow ( 10 , K)),
int (( pow ( 10 , K) - 1 ) / 9 )):
if (z > X):
break
ans + = int ((X - z) / int ( pow ( 10 , K)) + 1 )
return ans
def intCountInRange(L, R, K):
return (intCount(R, K) - intCount(L - 1 , K))
L = 49
R = 101
K = 2
print (intCountInRange(L, R, K))
|
C#
using System;
class GFG{
static int intCount( int X, int K)
{
int ans = 0;
for ( int z = 0; z < Math.Pow(10, K);
z += (( int )Math.Pow(10, K) - 1) / 9) {
if (z > X)
break ;
ans += ((X - z) / ( int )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;
Console.Write(intCountInRange(L, R, K));
}
}
|
Javascript
<script>
function intCount(X, K)
{
let ans = 0;
for (let z = 0; z < Math.pow(10, K);
z += Math.floor((Math.pow(10, K) - 1) / 9))
{
if (z > X)
break ;
ans += Math.floor(((X - z) /
Math.pow(10, K) + 1));
}
return ans;
}
function intCountInRange(L, R, K)
{
return (intCount(R, K) -
intCount(L - 1, K));
}
let L = 49;
let R = 101;
let K = 2;
document.write(intCountInRange(L, R, K));
</script>
|
Time Complexity: O(log K)
Space Complexity: O(1)
Share your thoughts in the comments
Please Login to comment...