Given two positive integers **N** and **M**, the task is to find the count of all possible numbers in the range **[1, M]**, having suffix as **N**.

**Examples:**

Input:N = 5, M = 15Output:2Explanation:Only numbers satisfying the conditions are {5, 15}.Input:N = 25, M = 4500Output: 45

**Naive Approach: **The simplest approach is to traverse all integers in the range **[1, M]** and check if the suffix is **N** or not. **Time Complexity:** O(M) **Auxiliary Space:** O(1)

**Efficient Approach:** To optimize the above approach, following observation needs to be made:

Let

N= 5 andM= 100

The Suffix numbers are 5, 15, 25, 35…95, which forms anArithmetic Progressionwith

first term = 5, last term = 95, common difference = Base of N (eg: 6 has base 10, 45 has base 100 which is nothing but the exponentiation of the form 10^{digitsOf(N)}, where digitsOf(N) = no. of digits present in N.

Therefore, in order to calculate the count of possible numbers in the range [1, M], the following expression needs to be evaluated:

Count of numbers = Number of terms in the series =

(t, where_{n}– a)/d + 1tis the last term of the sequence,_{n}ais the first term of the sequence,dis the common difference =(t_{i+1}– t_{i}), i = 1, 2, 3…n-1

Below is the implementation of the above approach:

## C++

`// C++ Program to implement` `// the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to count the` `// no. of digits of N` `int` `digitsOf(` `int` `num)` `{` ` ` `return` `to_string(num).size();` `}` `// Function to count all possible` `// numbers having Suffix as N` `int` `count(` `int` `a, ` `int` `tn)` `{` ` ` `// Difference of the A.P` ` ` `int` `diff = ` `pow` `(10, digitsOf(a));` ` ` `// Count of the number of terms` ` ` `return` `((tn - a) / diff) + 1;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `n, m;` ` ` `n = 25, m = 4500;` ` ` `cout << count(n, m);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement ` `// the above approach ` `import` `java.util.*;` `class` `GFG{` `// Function to count the` `// no. of digits of N` `static` `int` `digitsOf(` `int` `num)` `{` ` ` `return` `Integer.toString(num).length();` `}` `// Function to count all possible` `// numbers having Suffix as N` `static` `int` `count(` `int` `a, ` `int` `tn)` `{` ` ` ` ` `// Difference of the A.P` ` ` `int` `diff = (` `int` `)Math.pow(` `10` `, digitsOf(a));` ` ` `// Count of the number of terms` ` ` `return` `((tn - a) / diff) + ` `1` `;` `}` `// Driver code` `public` `static` `void` `main (String[] args)` `{` ` ` `int` `n = ` `25` `, m = ` `4500` `;` ` ` ` ` `System.out.println(count(n, m));` `}` `}` `// This code is contributed by offbeat` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement ` `# the above approach ` `# Function to count the` `# no. of digits of N` `def` `digitsOf(num):` ` ` `return` `len` `(` `str` `(num));` `# Function to count all possible` `# numbers having Suffix as N` `def` `count(a, tn):` ` ` `# Difference of the A.P` ` ` `diff ` `=` `int` `(` `pow` `(` `10` `, digitsOf(a)));` ` ` `# Count of the number of terms` ` ` `return` `((tn ` `-` `a) ` `/` `diff) ` `+` `1` `;` `# Driver code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `n ` `=` `25` `; m ` `=` `4500` `;` ` ` `print` `(` `int` `(count(n, m)));` `# This code is contributed by sapnasingh4991` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement ` `// the above approach ` `using` `System;` `class` `GFG{` `// Function to count the` `// no. of digits of N` `static` `int` `digitsOf(` `int` `num)` `{` ` ` `return` `num.ToString().Length;` `}` `// Function to count all possible` `// numbers having Suffix as N` `static` `int` `count(` `int` `a, ` `int` `tn)` `{` ` ` ` ` `// Difference of the A.P` ` ` `int` `diff = (` `int` `)Math.Pow(10, digitsOf(a));` ` ` `// Count of the number of terms` ` ` `return` `((tn - a) / diff) + 1;` `}` `// Driver code` `public` `static` `void` `Main(String[] args)` `{` ` ` `int` `n = 25, m = 4500;` ` ` ` ` `Console.WriteLine(count(n, m));` `}` `}` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

45

**Time Complexity:** O(1) **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:

- Construct a Matrix with no element exceeding X and sum of two adjacent elements not exceeding Y
- Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K
- Maximize count of pairs (i, j) from two arrays having element from first array not exceeding that from second array
- Count number of triangles possible with length of sides not exceeding N
- Maximize subsequences having array elements not exceeding length of the subsequence
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K | Set 2
- Maximum possible sum of non-adjacent array elements not exceeding K
- Lexicographically smallest permutation of size A having B integers exceeding all preceeding integers
- Minimize count of Subsets with difference between maximum and minimum element not exceeding K
- Count array elements with rank not exceeding K
- Split array into minimum number of subarrays having GCD of its first and last element exceeding 1
- Count of possible arrays from prefix-sum and suffix-sum arrays
- Maximum students to pass after giving bonus to everybody and not exceeding 100 marks
- Count the numbers with N digits and whose suffix is divisible by K
- Count numbers from a given range that contains a given number as the suffix
- Minimize count of flips required such that no substring of 0s have length exceeding K
- Count array elements exceeding sum of preceding K elements

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.