Count numbers from a given range that contains a given number as the suffix
Last Updated :
16 Apr, 2021
Given three integers A, L, and R, the task is to count numbers from a range L to R which contains A as its suffix.
Examples:
Input: A = 2, L = 2, R = 20
Output: 2
Explanation:
Only two possible numbers from the given range that satisfies the conditions are 2 and 12.
Input: A = 25, L = 25, R = 273
Output: 3
Explanation:
The three numbers from the given range that satisfies the conditions are 25, 125 and 225.
Naive Approach: The simplest approach to solve the problem is to traverse the numbers from the range L to R, and check if the number ends with A or not. For all numbers found to be true, increment the count of such numbers by 1. Finally, print the final count.
Time complexity: O(B)
Auxiliary Space: O(log2R)
Efficient Approach: To optimize the above approach, follow the steps below:
- Count and store the number of digits of A and store it in a variable, say, count.
- Raise 10 to the power of count of digits of A,i.e. 10count.
- Check for every cycle of 10count if it lies in the range [L, R]. If found to be true, then increment the count by 1.
- Print the final value of count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countNumEnds( int A, int L, int R)
{
int temp, count = 0, digits;
int cycle;
digits = log10 (A) + 1;
temp = pow (10, digits);
cycle = temp;
while (temp <= R) {
if (temp >= L)
count++;
temp += cycle;
}
cout << count;
}
int main()
{
int A = 2, L = 2, R = 20;
countNumEnds(A, L, R);
}
|
Java
class GFG{
static void countNumEnds( int A,
int L, int R)
{
int temp, count = 0 , digits;
int cycle;
digits = ( int ) (Math.log10(A) + 1 );
temp = ( int ) Math.pow( 10 , digits);
cycle = temp;
while (temp <= R)
{
if (temp >= L)
count++;
temp += cycle;
}
System.out.print(count);
}
public static void main(String[] args)
{
int A = 2 , L = 2 , R = 20 ;
countNumEnds(A, L, R);
}
}
|
Python3
from math import log10
def countNumEnds(A, L, R):
count = 0
digits = int (log10(A) + 1 )
temp = int ( pow ( 10 , digits))
cycle = temp
while (temp < = R):
if (temp > = L):
count + = 1
temp + = cycle
print (count)
A = 2
L = 2
R = 20
countNumEnds(A, L, R)
|
C#
using System;
class GFG{
static void countNumEnds( int A, int L,
int R)
{
int temp, count = 0, digits;
int cycle;
digits = ( int )(Math.Log10(A) + 1);
temp = ( int )Math.Pow(10, digits);
cycle = temp;
while (temp <= R)
{
if (temp >= L)
count++;
temp += cycle;
}
Console.Write(count);
}
public static void Main(String[] args)
{
int A = 2, L = 2, R = 20;
countNumEnds(A, L, R);
}
}
|
Javascript
<script>
function countNumEnds(A, L, R)
{
let temp = 0, count = 0, digits = 0;
let cycle = 0;
digits = Math.round(Math.log10(A)) + 1;
temp = Math.round(Math.pow(10, digits));
cycle = temp;
while (temp <= R)
{
if (temp >= L)
count++;
temp += cycle;
}
document.write(count);
}
let A = 2, L = 2, R = 20;
countNumEnds(A, L, R);
</script>
|
Time Complexity: O(N), where N is the range.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...