Open In App

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

Last Updated : 04 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// 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;
}


Java




// 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


Python3




# 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


C#




// 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


Javascript




<script>
// Javascript Program to implement
// the above approach
 
// Function to count the numbers
// ending with M
function getCount(N, M){
 
    // Stores the count of
    // numbers required
    let total_count = 0
 
    // Calculate count upto
    // nearest power of 10
    total_count += Math.floor(N / 10)
 
    // Computing the value of x
    let x = Math.floor(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
let N = 100
let M = 1
 
// Function call
document.write(getCount(N, M))
 
// This code is contributed by Saurabh Jaiswal
</script>


 
 

Output: 

10

 

Time complexity: O(1) 
Auxiliary Space: O(1) 

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads