Count numbers with unit digit k in given range

Here given a range from low to high and given a number k.You have to find out the number of count which a number has same digit as k

Examples:

Input : low = 2, high = 35, k = 2   
Output : 4
Numbers are 2, 12, 22, 32.  

Input  : low = 3, high = 30, k = 3 
Output : 3
Numbers are  3, 13, 23.



A naive approach is to traverse through all numbers in given range and check last digit of every number and increment result if last digit is equal to k.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Simple CPP program to count numbers with 
// last digit as k in given range.
#include <bits/stdc++.h>
using namespace std;
  
// Returns count of numbers with k as last
// digit.
int counLastDigitK(int low, int high, int k)
{
    int count = 0;
    for (int i = low; i <= high; i++) 
        if (i % 10 == k)
            count++;   
    return count;
}
  
// Driver Program
int main()
{
    int low = 3, high = 35, k = 3;
    cout << counLastDigitK(low, high, k);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Simple Java program to count numbers with 
// last digit as k in given range.
import java.util.*;
import java.lang.*;
  
public class GfG
{
    // Returns count of numbers with
    // k as last digit.
    public static int counLastDigitK(int low, 
                                int high, int k)
    {
        int count = 0;
        for (int i = low; i <= high; i++) 
            if (i % 10 == k)
                count++; 
        return count;
    }
      
    // driver function
    public static void main(String args[])
    {
        int low = 3, high = 35, k = 3;
        System.out.println(counLastDigitK(low, high, k));
    }
}
  
// This code is contributed by Sagar Shukla

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Simple python program to count numbers with 
# last digit as k in given range.
  
# Returns count of numbers with k as last
# digit.
def counLastDigitK(low, high, k):
    count = 0
    for i in range(low, high+1):
        if (i % 10 == k):
            count+=1 
    return count
  
  
# Driver Program
low = 3
high = 35
k = 3
print(counLastDigitK(low, high, k))
  
# This code is contributed by
# Smitha Dinesh Semwal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// Simple C# program to count numbers with 
// last digit as k in given range.
using System;
  
public class GfG
{
    // Returns count of numbers with
    // k as last digit.
    public static int counLastDigitK(int low, 
                                int high, int k)
    {
        int count = 0;
        for (int i = low; i <= high; i++) 
            if (i % 10 == k)
                count++; 
        return count;
    }
      
    // Driver function
    public static void Main()
    {
        int low = 3, high = 35, k = 3;
        Console.WriteLine(counLastDigitK(low, high, k));
    }
}
  
// This code is contributed by vt_m

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Simple PHP program to count numbers with 
// last digit as k in given range.
  
// Returns count of numbers with
// k as last digit.
function counLastDigitK($low, $high, $k)
{
    $count = 0;
    for ($i = $low; $i <= $high; $i++) 
        if ($i % 10 == $k)
            $count++; 
    return $count;
}
  
    // Driver Code
    $low = 3;
    $high = 35; 
    $k = 3;
    echo counLastDigitK($low, $high, $k);
      
// This code is contributed by ajit
?>

chevron_right



Output:

 4

Time Complexity : O(high – low)

 

An efficient solution is based on the fact that every digit appears once as last digit in every 10 consecutive numbers.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Efficient CPP program to count numbers  
// with last digit as k in given range.
#include <bits/stdc++.h>
using namespace std;
  
// Returns count of numbers with k as last
// digit.
int counLastDigitK(int low, int high, int k)
{
    int count = (high - low)/10;
    if (high % 10 >= k)
        count++;
    if (low % 10 > k)
        count--;
    return count;
}
  
// Driver Program
int main()
{
    int low = 3, high = 35, k = 3;
    cout << counLastDigitK(low, high, k);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Efficient Java program to count numbers 
// with last digit as k in given range.
import java.util.*;
import java.lang.*;
  
public class GfG
{
    // Returns count of numbers with 
    // k as last digit.
    public static int counLastDigitK(int low, 
                                 int high, int k)
    {
        int count = (high - low)/10;
        if (high % 10 >= k)
            count++;
        if (low % 10 > k)
            count--;
        return count;
    }
      
    // driver function
    public static void main(String argc[])
    {
        int low = 3, high = 35, k = 3;
        System.out.println(counLastDigitK(low, high, k));
    }
}
  
// This code is contributed by Sagar Shukla

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Efficient python program to count numbers 
# with last digit as k in given range.
  
# Returns count of numbers with k as last
# digit.
def counLastDigitK(low, high, k):
    count = (high - low)/10
    if (high % 10 >= k):
        count+=1
    if (low % 10 > k):
        count-=1
    return int(count)
  
  
# Driver Program
low = 3
high = 35
k = 3
print(counLastDigitK(low, high, k))
  
# This code is contributed by
# Smitha Dinesh Semwal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// Efficient Java program to count numbers 
// with last digit as k in given range.
using System;
  
public class GfG
{
    // Returns count of numbers with 
    // k as last digit.
    public static int counLastDigitK(int low, 
                                int high, int k)
    {
        int count = (high - low) / 10;
        if (high % 10 >= k)
            count++;
        if (low % 10 > k)
            count--;
        return count;
    }
      
    // Driver function
    public static void Main()
    {
        int low = 3, high = 35, k = 3;
        Console.WriteLine(counLastDigitK(low, high, k));
    }
}
  
// This code is contributed by vt_m

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Efficient PHP program to 
// count numbers with last 
// digit as k in given range.
  
// Returns count of numbers
// with k as last digit.
function counLastDigitK($low
                        $high, $k)
{
    $count = ($high - $low) / 10;
    if ($high % 10 >= $k)
        $count++;
    if ($low % 10 > $k)
        $count--;
    return floor($count);
}
  
// Driver Code
$low = 3;
$high = 35;
$k = 3;
echo counLastDigitK($low
                    $high, $k);
  
// This code is contributed
// by ajit
?>

chevron_right



Output:

 4

Time Complexity : O(1)



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.



Improved By : jit_t