Open In App

Count n digit numbers not having a particular digit

Last Updated : 06 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

We are given two integers n and d, we need to count all n digit numbers that do not have digit d.

Example : 

Input : n = 2, d = 7
Output : 72
All two digit numbers that don't have
7 as digit are 10, 11, 12, 13, 14, 15,
16, 18, .....

Input : n = 3, d = 9
Output : 648
 

A simple solution is to traverse through all d digit numbers. For every number, check if it has x as digit or not. 

Efficient approach In this method, we check first if excluding digit d is zero or non-zero. If it is zero then, we have 9 numbers (1 to 9) as first number otherwise we have 8 numbers(excluding x digit and 0). Then for all other digits, we have 9 choices i.e (0 to 9 excluding d digit). We simple call digitNumber function with n-1 digits as first number we already find it can be 8 or 9 and simple multiply it. For other numbers we check if digits are odd or even if it is odd we call digitNumber function with (n-1)/2 digits and multiply it by 9 otherwise we call digitNumber function with n/2 digits and store them in result and take result square.

Illustration :  

Number from 1 to 7 excluding digit 9.
digits      multiple          number
1           8                   8
2           8*9                 72
3           8*9*9               648 
4           8*9*9*9             5832
5           8*9*9*9*9           52488
6           8*9*9*9*9*9         472392
7           8*9*9*9*9*9*9       4251528

As we can see, in each step we are half the number of digits. Suppose we have 7 digits in this we call function from main with 6(7-1) digits. when we half the digits we left with 3(6/2) digits. Because of this we have to multiply result due to 3 digits with itself to get result for 6 digits. Similarly for 3 digits we have odd digits, we have odd digits. We find result with 1 ((3-1)/2) digits and find result square and multiply it with 9, because we find result for d-1 digits. 

C++




// C++ Implementation of above method
#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
 
// Finding number of possible number with
// n digits excluding a particular digit
long long digitNumber(long long n) {
 
  // Checking if number of digits is zero
  if (n == 0)
    return 1;
 
  // Checking if number of digits is one
  if (n == 1)
    return 9;
 
  // Checking if number of digits is odd
  if (n % 2) {
 
    // Calling digitNumber function
    // with (digit-1)/2 digits
    long long temp = digitNumber((n - 1) / 2) % mod;
    return (9 * (temp * temp) % mod) % mod;
  } else {
 
    // Calling digitNumber function
    // with n/2 digits
    long long temp = digitNumber(n / 2) % mod;
    return (temp * temp) % mod;
  }
}
 
int countExcluding(int n, int d)
{
  // Calling digitNumber function
  // Checking if excluding digit is
  // zero or non-zero
  if (d == 0)
    return (9 * digitNumber(n - 1)) % mod;
  else
    return (8 * digitNumber(n - 1)) % mod;
}
 
// Driver function to run above program
int main() {
 
  // Initializing variables
  long long d = 9;
  int n = 3;
  cout << countExcluding(n, d) << endl;
  return 0;
}


Java




// Java Implementation of above method
import java.lang.*;
 
class GFG {
     
static final int mod = 1000000007;
 
// Finding number of possible number with
// n digits excluding a particular digit
static int digitNumber(long n) {
 
    // Checking if number of digits is zero
    if (n == 0)
    return 1;
 
    // Checking if number of digits is one
    if (n == 1)
    return 9;
 
    // Checking if number of digits is odd
    if (n % 2 != 0) {
 
    // Calling digitNumber function
    // with (digit-1)/2 digits
    int temp = digitNumber((n - 1) / 2) % mod;
     
    return (9 * (temp * temp) % mod) % mod;
    }
    else {
 
    // Calling digitNumber function
    // with n/2 digits
    int temp = digitNumber(n / 2) % mod;
     
    return (temp * temp) % mod;
    }
}
 
static int countExcluding(int n, int d) {
     
    // Calling digitNumber function
    // Checking if excluding digit is
    // zero or non-zero
    if (d == 0)
    return (9 * digitNumber(n - 1)) % mod;
    else
    return (8 * digitNumber(n - 1)) % mod;
}
 
// Driver function to run above program
public static void main(String[] args) {
     
    // Initializing variables
    int d = 9;
    int n = 3;
    System.out.println(countExcluding(n, d));
}
}
 
// This code is contributed by Anant Agarwal.


Python3




# Python Implementation
# of above method
 
mod=1000000007
 
# Finding number of
# possible number with
# n digits excluding
# a particular digit
def digitNumber(n):
 
    # Checking if number
    # of digits is zero
    if (n == 0):
        return 1
  
    # Checking if number
    # of digits is one
    if (n == 1):
        return 9
  
    # Checking if number
    # of digits is odd
    if (n % 2!=0):
  
        # Calling digitNumber function
        # with (digit-1)/2 digits
        temp = digitNumber((n - 1) // 2) % mod
        return (9 * (temp * temp) % mod) % mod
    else:
  
        # Calling digitNumber function
        # with n/2 digits
        temp = digitNumber(n // 2) % mod
        return (temp * temp) % mod
  
def countExcluding(n,d):
 
    # Calling digitNumber function
    # Checking if excluding digit is
    # zero or non-zero
    if (d == 0):
        return (9 * digitNumber(n - 1)) % mod
    else:
        return (8 * digitNumber(n - 1)) % mod
     
# Driver code
 
d = 9
n = 3
print(countExcluding(n, d))
 
# This code is contributed
# by Anant Agarwal.


C#




// C# Implementation of above method
using System;
 
class GFG {
     
    static int mod = 1000000007;
     
    // Finding number of possible number with
    // n digits excluding a particular digit
    static int digitNumber(long n) {
     
        // Checking if number of digits is zero
        if (n == 0)
            return 1;
     
        // Checking if number of digits is one
        if (n == 1)
            return 9;
     
        // Checking if number of digits is odd
        if (n % 2 != 0) {
     
            // Calling digitNumber function
            // with (digit-1)/2 digits
            int temp = digitNumber((n - 1) / 2) % mod;
             
            return (9 * (temp * temp) % mod) % mod;
        }
        else {
     
            // Calling digitNumber function
            // with n/2 digits
            int temp = digitNumber(n / 2) % mod;
             
            return (temp * temp) % mod;
        }
    }
     
    static int countExcluding(int n, int d) {
         
        // Calling digitNumber function
        // Checking if excluding digit is
        // zero or non-zero
        if (d == 0)
            return (9 * digitNumber(n - 1)) % mod;
        else
            return (8 * digitNumber(n - 1)) % mod;
    }
     
    // Driver function to run above program
    public static void Main() {
         
        // Initializing variables
        int d = 9;
        int n = 3;
         
        Console.WriteLine(countExcluding(n, d));
    }
}
 
// This code is contributed by vt_m.


PHP




<?php
// PHP Implementation
// of above method
 
$mod = 1000000007;
 
// Finding number of
// possible number with
// n digits excluding
// a particular digit
function digitNumber($n)
{
    global $mod;
     
    // Checking if number
    // of digits is zero
    if ($n == 0)
        return 1;
 
    // Checking if number
    // of digits is one
    if ($n == 1)
        return 9;
 
    // Checking if number
    // of digits is odd
    if ($n % 2 != 0)
    {
        // Calling digitNumber
        // function with
        // (digit-1)/2 digits;
        $temp = digitNumber(($n -
                    1) / 2) % $mod;
        return (9 * ($temp * $temp) %
                     $mod) % $mod;
    }
    else
    {
        // Calling digitNumber
        // function with n/2 digits
        $temp = digitNumber($n /
                      2) % $mod;
        return ($temp *
                $temp) % $mod;
    }
}
 
function countExcluding($n, $d)
{
    global $mod;
     
    // Calling digitNumber
    // function Checking if
    // excluding digit is
    // zero or non-zero
    if ($d == 0)
        return (9 * digitNumber($n -
                         1)) % $mod;
    else
        return (8 * digitNumber($n -
                         1)) % $mod;
}    
 
// Driver code
$d = 9;
$n = 3;
print(countExcluding($n, $d));
 
// This code is contributed by
// Manish Shaw(manishshaw1)
?>


Javascript




<script>
 
// JavaScript Implementation of above method
 
const mod = 1000000007;
 
 
// Finding number of possible number with
// n digits excluding a particular digit
function digitNumber(n) {
 
// Checking if number of digits is zero
if (n == 0)
    return 1;
 
// Checking if number of digits is one
if (n == 1)
    return 9;
 
// Checking if number of digits is odd
if (n % 2) {
 
    // Calling digitNumber function
    // with (digit-1)/2 digits
    let temp = digitNumber((n - 1) / 2) % mod;
    return (9 * (temp * temp) % mod) % mod;
} else {
 
    // Calling digitNumber function
    // with n/2 digits
    let temp = digitNumber(n / 2) % mod;
    return (temp * temp) % mod;
}
}
 
function countExcluding(n, d)
{
// Calling digitNumber function
// Checking if excluding digit is
// zero or non-zero
if (d == 0)
    return (9 * digitNumber(n - 1)) % mod;
else
    return (8 * digitNumber(n - 1)) % mod;
}
 
// Driver function to run above program
 
 
// Initializing variables
let d = 9;
let n = 3;
document.write(countExcluding(n, d) + "<br>");
 
 
// This code is contributed by Surbhi Tyagi.
 
</script>


Output: 

648

 

Time Complexity: O(log n), where n represents the given integer.
Auxiliary Space: O(logn), due to recursive stack space.



Similar Reads

Count of N-digit numbers having digit XOR as single digit
Given an integer N, the task is to find the total count of N-Digit numbers such that the Bitwise XOR of the digits of the numbers is a single digit. Examples: Input: N = 1Output: 9Explanation: 1, 2, 3, 4, 5, 6, 7, 8, 9 are the numbers. Input: N = 2Output: 66Explanation: There are 66 such 2-digit numbers whose Xor of digits is a single digit number.
14 min read
Count numbers in a range with digit sum divisible by K having first and last digit different
Given a range in the form of L and R, and a value K, the task is to count the numbers between the range L to R such that the sum of the digit is divisible by K, and the first digit is not equal to the last digit of the number. Examples: Input: L = 1, R = 10, K = 2 Output: 0 Explanation: The input numbers whose sum of the digits are divisible by 2 (
15 min read
Count of N-digit numbers having each digit as the mean of its adjacent digits
Given a positive integer N, the task is to count the number of N-digit numbers where each digit in the number is the mean of its two adjacent digits. Examples : Input: N = 1Output: 10Explanation: All numbers from 0 to 9 satisfy the given condition as there is only one digit. Input: N = 2Output: 90 Naive Approach: The simplest approach to solve the
11 min read
Count of N digit numbers not having given prefixes
Given an integer N and a vector of strings prefixes[], the task is to calculate the total possible strings of length N from characters '0' to '9'. such that the given prefixes cannot be used in any of the strings. Examples: Input: N = 3, prefixes = {"42"}Output: 990Explanation: All string except{"420", "421", "422", "423", "424", "425", "426", "427
8 min read
Count no. of ordered subsets having a particular XOR value
Given an array arr[] of n elements and a number K, find the number of ordered subsets of arr[] having XOR of elements as K This is a modified version of this problem. So it is recommended to try that problem before.Examples: Input: arr[] = {6, 9, 4, 2}, k = 6 Output: 2 The subsets are {4, 2}, {2, 4} and {6}Input: arr[] = {1, 2, 3}, k = 1 Output: 4
11 min read
Count number of subsets having a particular XOR value
Given an array arr[] of n numbers and a number K, find the number of subsets of arr[] having XOR of elements as KExamples : Input: arr[] = {6, 9, 4,2}, k = 6Output: 2The subsets are {4, 2} and {6}Input: arr[] = {1, 2, 3, 4, 5}, k = 4Output: 4The subsets are {1, 5}, {4}, {1, 2, 3, 4} and {2, 3, 5}We strongly recommend that you click here and practic
26 min read
Count possible N-digit numbers such that each digit does not appear more than given number of times consecutively
Given an integer N and an array maxDigit[], the task is to count all the distinct N-digit numbers such that digit i does not appear more than maxDigit[i] times. Since the count can be very large, print it modulo 109 + 7. Examples: Input: N = 2, maxDigit[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}Output: 90Explanation:Any digit can't appear more than once co
18 min read
Count the number of occurrences of a particular digit in a number
Given a number N and a digit D, the task is to count the occurrences of D in N.Examples: Input: N = 25, D = 2 Output: 1 Input: N = 100, D = 0 Output: 2 Naive Approach: The basic idea is to loop through the digits of the number and keep a count of the occurrences of each digit in a hash table. Then, we can simply look up the count of the digit we ar
7 min read
Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
Given integers A, B, and N, the task is to find the total number of N-digit numbers whose sum of digits at even positions and odd positions are divisible by A and B respectively. Examples: Input: N = 2, A = 2, B = 5Output: 5Explanation:The only 2-digit numbers {50, 52, 54, 56, 58} with sum of odd-positioned digits equal to 5 and even-positioned dig
16 min read
Count of N-digit numbers having equal count of distinct odd and even digits
Given a positive integer N, the task is to count the number of N-digit numbers such that the count of distinct odd and distinct even digits in the number is the same. Examples: Input: N = 2Output : 45Explanation:For a 2-digit number, in order to satisfy the condition, the first digit can be even and second digit odd, or the second digit can be odd
15 min read
Practice Tags :