Count numbers with unit digit k in given range
Last Updated :
17 Feb, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int countLastDigitK( int low, int high, int k)
{
int count = 0;
for ( int i = low; i <= high; i++)
if (i % 10 == k)
count++;
return count;
}
int main()
{
int low = 3, high = 35, k = 3;
cout << countLastDigitK(low, high, k);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GfG
{
public static int countLastDigitK( int low,
int high, int k)
{
int count = 0 ;
for ( int i = low; i <= high; i++)
if (i % 10 == k)
count++;
return count;
}
public static void main(String args[])
{
int low = 3 , high = 35 , k = 3 ;
System.out.println(countLastDigitK(low, high, k));
}
}
|
Python3
def countLastDigitK(low, high, k):
count = 0
for i in range (low, high + 1 ):
if (i % 10 = = k):
count + = 1
return count
low = 3
high = 35
k = 3
print (countLastDigitK(low, high, k))
|
C#
using System;
public class GfG
{
public static int countLastDigitK( int low,
int high, int k)
{
int count = 0;
for ( int i = low; i <= high; i++)
if (i % 10 == k)
count++;
return count;
}
public static void Main()
{
int low = 3, high = 35, k = 3;
Console.WriteLine(countLastDigitK(low, high, k));
}
}
|
PHP
<?php
function countLastDigitK( $low , $high , $k )
{
$count = 0;
for ( $i = $low ; $i <= $high ; $i ++)
if ( $i % 10 == $k )
$count ++;
return $count ;
}
$low = 3;
$high = 35;
$k = 3;
echo countLastDigitK( $low , $high , $k );
?>
|
Javascript
<script>
function countLastDigitK(low, high, k)
{
let count = 0;
for (let i = low; i <= high; i++)
if (i % 10 == k)
count++;
return count;
}
let low = 3;
let high = 35;
let k = 3;
document.write( countLastDigitK(low, high, k));
</script>
|
Output:
4
Time Complexity: O(high – low)
Auxiliary Space: O(1)
An efficient solution is based on the fact that every digit appears once as the last digit in every 10 consecutive numbers.
C++
#include <bits/stdc++.h>
using namespace std;
int countLastDigitK( long long low,
long long high, long long K)
{
long long mlow = 10 * ceil (low/10.0);
long long mhigh = 10 * floor (high/10.0);
int count = (mhigh - mlow)/10;
if (high % 10 >= K)
count++;
if (low % 10 <=K && (low%10))
count++;
return count;
}
int main()
{
int low = 3, high = 35, k = 3;
cout << countLastDigitK(low, high, k);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GfG
{
public static int counLastDigitK( int low,
int high, int k)
{
int mlow = 10 * ( int )
Math.ceil(low/ 10.0 );
int mhigh = 10 * ( int )
Math.floor(high/ 10.0 );
int count = (mhigh - mlow)/ 10 ;
if (high % 10 >= k)
count++;
if (low % 10 <= k && (low% 10 ) > 0 )
count++;
return count;
}
public static void main(String argc[])
{
int low = 3 , high = 35 , k = 3 ;
System.out.println(counLastDigitK(low, high, k));
}
}
|
Python3
import math
def counLastDigitK(low, high, k):
mlow = 10 * math.ceil(low / 10.0 )
mhigh = 10 * int (high / 10.0 )
count = (mhigh - mlow) / 10
if (high % 10 > = k):
count + = 1
if (low % 10 < = k and \
(low % 10 ) > 0 ):
count + = 1
return int (count)
low = 3
high = 35
k = 3
print (counLastDigitK(low, high, k))
|
C#
using System;
public class GfG
{
public static int counLastDigitK( int low,
int high, int k)
{
int mlow = 10 * Convert.ToInt32(
Math.Ceiling(low/10.0));
int mhigh = 10 * Convert.ToInt32(
Math.Floor(high/10.0));
int count = (mhigh - mlow) / 10;
if (high % 10 >= k)
count++;
if (low % 10 <= k && (low%10) > 0)
count++;
return count;
}
public static void Main()
{
int low = 3, high = 35, k = 3;
Console.WriteLine(
counLastDigitK(low, high, k));
}
}
|
Javascript
<script>
function counLastDigitK(low, high, k)
{
let mlow = 10 * (Math.ceil(low/10.0));
let mhigh = 10 * (Math.floor(high/10.0));
let count = (mhigh - mlow) / 10;
if (high % 10 >= k)
count++;
if (low % 10 <= k && (low%10) > 0)
count++;
return count;
}
let low = 3, high = 35, k = 3;
document.write(counLastDigitK(low, high, k));
</script>
|
Time Complexity : O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...