Given a number n, write a function that returns count of numbers from 1 to n that don’t contain digit 3 in their decimal representation.
Examples:
Input: n = 10
Output: 9
Input: n = 45
Output: 31
// Numbers 3, 13, 23, 30, 31, 32, 33, 34,
// 35, 36, 37, 38, 39, 43 contain digit 3.
Input: n = 578
Output: 385
Solution:
We can solve it recursively. Let count(n) be the function that counts such numbers.
'msd' --> the most significant digit in n
'd' --> number of digits in n.
count(n) = n if n < 3
count(n) = n - 1 if 3 <= n 10 and msd is not 3
count(n) = count( msd * (10^(d-1)) - 1)
if n > 10 and msd is 3
Let us understand the solution with n = 578.
count(578) = 4*count(99) + 4 + count(78)
The middle term 4 is added to include numbers
100, 200, 400 and 500.
Let us take n = 35 as another example.
count(35) = count (3*10 - 1) = count(29)
C++
#include <bits/stdc++.h>
using namespace std;
int count( int n)
{
if (n < 3)
return n;
if (n >= 3 && n < 10)
return n-1;
int po = 1;
while (n/po > 9)
po = po*10;
int msd = n/po;
if (msd != 3)
return count(msd)*count(po - 1) + count(msd) + count(n%po);
else
return count(msd*po - 1);
}
int main()
{
cout << count(578) << " " ;
return 0;
}
|
C
#include <stdio.h>
int count( int n)
{
if (n < 3)
return n;
if (n >= 3 && n < 10)
return n-1;
int po = 1;
while (n/po > 9)
po = po*10;
int msd = n/po;
if (msd != 3)
return count(msd)*count(po - 1) + count(msd) + count(n%po);
else
return count(msd*po - 1);
}
int main()
{
printf ( "%d " , count(578));
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int count( int n)
{
if (n < 3 )
return n;
if (n >= 3 && n < 10 )
return n- 1 ;
int po = 1 ;
while (n/po > 9 )
po = po* 10 ;
int msd = n/po;
if (msd != 3 )
return count(msd)*count(po - 1 ) + count(msd) + count(n%po);
else
return count(msd*po - 1 );
}
public static void main (String[] args)
{
int n = 578 ;
System.out.println(count(n));
}
}
|
Python3
def count(n):
if n < 3 :
return n
elif n > = 3 and n < 10 :
return n - 1
po = 1
while n / / po > 9 :
po = po * 10
msd = n / / po
if msd ! = 3 :
return count(msd) * count(po - 1 ) + count(msd) + count(n % po)
else :
return count(msd * po - 1 )
n = 578
print (count(n))
|
C#
using System;
class GFG {
static int count( int n)
{
if (n < 3)
return n;
if (n >= 3 && n < 10)
return n-1;
int po = 1;
while (n / po > 9)
po = po * 10;
int msd = n / po;
if (msd != 3)
return count(msd) * count(po - 1)
+ count(msd) + count(n % po);
else
return count(msd * po - 1);
}
public static void Main ()
{
int n = 578;
Console.Write(count(n));
}
}
|
PHP
<?php
function count1( $n )
{
if ( $n < 3)
return $n ;
if ( $n >= 3 && $n < 10)
return $n -1;
$po = 1;
for ( $x = intval ( $n / $po ); $x > 9; $x = intval ( $n / $po ))
$po = $po *10;
$msd = intval ( $n / $po );
if ( $msd != 3)
return count1( $msd ) * count1( $po - 1) +
count1( $msd ) + count1( $n % $po );
else
return count1( $msd * $po - 1);
}
echo count1(578);
?>
|
Javascript
<script>
function count(n)
{
if (n < 3)
return n;
if (n >= 3 && n < 10)
return n - 1;
var po = 1;
while (parseInt(n / po) > 9)
po = po * 10;
var msd = parseInt (n / po);
if (msd != 3)
return count(msd) * count(po - 1) + count(msd) + count(n % po);
else
return count(msd * po - 1);
}
var n = 578;
document.write(count(n));
</script>
|
Output:
385
Time Complexity: O(log10n)
Auxiliary Space: O(1), since no extra space has been taken.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above