Count all numbers up to N having M as the last digit

Given two positive numbers M and N, the task is to find the count of all numbers having M as the last digit from the range [1, N].

Examples: 

Input: M = 5, N = 15 
Output:
Explanation: 
Only 2 numbers(5 and 15) from the range [1, 15] ends with the digit ‘5’.
Input: M = 1, N = 100 
Output: 10

Naive Approach: The simplest approach is to iterate over the range 1 to N and check if the last digit is equal to M or not. If found to be true, then increment the count. Finally, print the count obtained. 
Time complexity: O(N) 
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is based on the fact that the count of numbers ending with every digit will be the same until the largest multiple of 10 which is less than N (say x). Therefore, its count will be (N / 10). Now, the task is reduced to compute the count of numbers ending with M which are between x and N.
Below are the steps:

  • Initialize a variable to store the total count, say total_count.
  • Add (N / 10) to the total count.
  • Compute x to store the largest multiple of 10 which is less than N using the formula:

 
 



x = (N / 10) * 10

 

  • Now, calculate the count of numbers ending with M lying in between x and N.
  • Add this count to the total_count. Print the final value of the total_count obtained.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to implement
// the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to count the numbers
// ending with M
int getCount(int N, int M)
{
  
    // Stores the count of
    // numbers required
    int total_count = 0;
  
    // Calculate count upto
    // nearest power of 10
    total_count += (N / 10);
  
    // Computing the value of x
    int x = (N / 10) * 10;
  
    // Adding the count of numbers
    // ending at M from x to N
    if ((N - x) >= M) 
    {
        total_count = total_count + 1;
    }
    return total_count;
}
  
// Driver Code
int main()
{
    int N = 100, M = 1;
  
    // Function Call
    cout << getCount(N, M);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above approach
import java.util.*;
  
class GFG{
  
// Function to count the numbers
// ending with M
static int getCount(int N, int M)
{
  
    // Stores the count of
    // numbers required
    int total_count = 0;
  
    // Calculate count upto
    // nearest power of 10
    total_count += (N / 10);
  
    // Computing the value of x
    int x = (N / 10) * 10;
  
    // Adding the count of numbers
    // ending at M from x to N
    if ((N - x) >= M) 
    {
        total_count = total_count + 1;
    }
    return total_count;
}
  
// Driver Code
public static void main(String[] args)
{
    int N = 100, M = 1;
  
    // Function call
    System.out.print(getCount(N, M));
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Program to implement
# the above approach
  
# Function to count the numbers
# ending with M 
def getCount(N, M):
  
    # Stores the count of
    # numbers required
    total_count = 0
  
    # Calculate count upto
    # nearest power of 10
    total_count += N // 10
  
    # Computing the value of x
    x = (N // 10) * 10
  
    # Adding the count of numbers
    # ending at M from x to N
    if((N - x) >= M):
        total_count = total_count + 1
  
    return total_count
  
# Driver Code
N = 100
M = 1
  
# Function call
print(getCount(N, M))
  
# This code is contributed by Shivam Singh

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to implement
// the above approach
using System;
class GFG{
  
    // Function to count the 
    // numbers ending with M
    static int getCount(int N, int M)
    {
  
        // Stores the count of
        // numbers required
        int total_count = 0;
  
        // Calculate count upto
        // nearest power of 10
        total_count += (N / 10);
  
        // Computing the value of x
        int x = (N / 10) * 10;
  
        // Adding the count of numbers
        // ending at M from x to N
        if ((N - x) >= M) 
        {
            total_count = total_count + 1;
        }
        return total_count;
    }
  
    // Driver Code
    public static void Main(String[] args)
    {
        int N = 100, M = 1;
  
        // Function call
        Console.Write(getCount(N, M));
    }
}
  
// This code is contributed by shikhasingrajput

chevron_right


Output: 

10



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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.