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

Only two possible numbers from the given range that satisfies the conditions are2and12.

Input:A = 25, L = 25, R = 273Output:3Explanation:

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(log_{2}R)

**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. 10^{count}. - Check for every cycle of 10
^{count}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++

`// C++ Program of the ` `// above approach ` ` ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// Function to count the number ` `// ends with given number in range ` `void` `countNumEnds(` `int` `A, ` `int` `L, ` `int` `R) ` `{ ` ` ` `int` `temp, count = 0, digits; ` ` ` `int` `cycle; ` ` ` ` ` `// Find number of digits in A ` ` ` `digits = ` `log10` `(A) + 1; ` ` ` ` ` `// Find the power of 10 ` ` ` `temp = ` `pow` `(10, digits); ` ` ` `cycle = temp; ` ` ` ` ` `while` `(temp <= R) { ` ` ` ` ` `if` `(temp >= L) ` ` ` `count++; ` ` ` ` ` `// Incrementing the A ` ` ` `temp += cycle; ` ` ` `} ` ` ` ` ` `cout << count; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `A = 2, L = 2, R = 20; ` ` ` ` ` `// Function Call ` ` ` `countNumEnds(A, L, R); ` `}` |

*chevron_right*

*filter_none*

## Java

`// Java Program of the ` `// above approach ` `class` `GFG{ ` ` ` `// Function to count the number ` `// ends with given number in range ` `static` `void` `countNumEnds(` `int` `A, ` ` ` `int` `L, ` `int` `R) ` `{ ` ` ` `int` `temp, count = ` `0` `, digits; ` ` ` `int` `cycle; ` ` ` ` ` `// Find number of digits in A ` ` ` `digits = (` `int` `) (Math.log10(A) + ` `1` `); ` ` ` ` ` `// Find the power of 10 ` ` ` `temp = (` `int` `) Math.pow(` `10` `, digits); ` ` ` `cycle = temp; ` ` ` ` ` `while` `(temp <= R) ` ` ` `{ ` ` ` `if` `(temp >= L) ` ` ` `count++; ` ` ` ` ` `// Incrementing the A ` ` ` `temp += cycle; ` ` ` `} ` ` ` `System.out.print(count); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `A = ` `2` `, L = ` `2` `, R = ` `20` `; ` ` ` ` ` `// Function Call ` ` ` `countNumEnds(A, L, R); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program of the ` `# above approach ` `from` `math ` `import` `log10 ` ` ` `# Function to count the number ` `# ends with given number in range ` `def` `countNumEnds(A, L, R): ` ` ` ` ` `count ` `=` `0` ` ` ` ` `# Find number of digits in A ` ` ` `digits ` `=` `int` `(log10(A) ` `+` `1` `) ` ` ` ` ` `# Find the power of 10 ` ` ` `temp ` `=` `int` `(` `pow` `(` `10` `, digits)) ` ` ` `cycle ` `=` `temp ` ` ` ` ` `while` `(temp <` `=` `R): ` ` ` `if` `(temp >` `=` `L): ` ` ` `count ` `+` `=` `1` ` ` ` ` `# Incrementing the A ` ` ` `temp ` `+` `=` `cycle ` ` ` ` ` `print` `(count) ` ` ` `# Driver Code ` `A ` `=` `2` `L ` `=` `2` `R ` `=` `20` ` ` `# Function call ` `countNumEnds(A, L, R) ` ` ` `# This code is contributed by Shivam Singh ` |

*chevron_right*

*filter_none*

## C#

`// C# program of the ` `// above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to count the number ` `// ends with given number in range ` `static` `void` `countNumEnds(` `int` `A, ` `int` `L, ` ` ` `int` `R) ` `{ ` ` ` `int` `temp, count = 0, digits; ` ` ` `int` `cycle; ` ` ` ` ` `// Find number of digits in A ` ` ` `digits = (` `int` `)(Math.Log10(A) + 1); ` ` ` ` ` `// Find the power of 10 ` ` ` `temp = (` `int` `)Math.Pow(10, digits); ` ` ` `cycle = temp; ` ` ` ` ` `while` `(temp <= R) ` ` ` `{ ` ` ` `if` `(temp >= L) ` ` ` `count++; ` ` ` ` ` `// Incrementing the A ` ` ` `temp += cycle; ` ` ` `} ` ` ` `Console.Write(count); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `A = 2, L = 2, R = 20; ` ` ` ` ` `// Function call ` ` ` `countNumEnds(A, L, R); ` `} ` `} ` ` ` `// This code is contributed by Amit Katiyar` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity: **O(N), where N is the range. **Auxiliary Space: **O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of numbers from the range [L, R] which contains at least one digit that divides K
- Count the numbers with N digits and whose suffix is divisible by K
- Count of all possible numbers not exceeding M having suffix N
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Count of possible arrays from prefix-sum and suffix-sum arrays
- Check if an array contains all elements of a given range
- Find strings that end with a given suffix
- Count of subarrays which contains a given number exactly K times
- Check if a string is suffix of another
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Longest suffix such that occurrence of each character is less than N after deleting atmost K characters
- Maximize the sum of array after multiplying a prefix and suffix by -1
- Longest prefix which is also suffix
- Program to find count of numbers having odd number of divisors in given range
- Queries to find the count of shortest paths in a Tree that contains a given edge
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Find the numbers from 1 to N that contains exactly k non-zero digits

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.