Horje
Count of N digit numbers with at least one digit as K

Given a number N and a digit K, The task is to count N digit numbers with at least one digit as K.

Examples:

Input: N = 3, K = 2
Output: 252
Explanation: 
For one occurrence of 2 – 
In a number of length 3, the following cases are possible:
=>When first digit is 2 and other two digits can have 9 values except ‘2’. 
Thus 9*9 = 81 combination are possible.
=> When second digit is 2 and first digit can have 8 values from 1 to 9 except ‘2’ 
and the third digit can have 9 value from 0 to 9 except ‘2’.
Thus 8*9 = 72 valid combination.
=>When third digit is 2 the first digit can have 8 values from 1 to 9 except ‘2’ 
and the second digit can have 9 values from 0 to 9 except ‘2’ thus 8*9 = 72. 
Hence total valid combination with one occurrence of 2 = 72 + 72 + 81 = 225.
For two occurrence of 2 – 
First and second digit can be 2 and third digit can have 9 values from 0 to 9. 
Second and third digit can have value 2 and first digit 
can have  8 values from 1 to 9 except 2. 
First and third digit can have values 2 and second digit 
can have 9 values from 0 to 9 except 2. 
Hence total valid combination with two occurrence of 2 = 9 + 8 + 9 = 26.
For all three digits to be 2 – 
There can be only 1 combination.
Hence total possible numbers with at least one occurrence of 2 =  225 + 26 + 1 = 252.

Input: N = 9, K = 8
Output: 555626232

 

Approach: The problem can be solved based on the following mathematical idea:

Find the difference between count of unique N digit numbers possible and count of all unique N digit numbers with no occurrence of digit K.

Follow the steps mentioned below to implement this idea: 

  • Find the count of all N digits numbers = 9 x 10N-1, Leftmost place can be any digit from 1-9, other digits can have any value from between 0 and 9.
  • Find the count of all N digits number with no occurrence of K = 8 x 9n-1, Leftmost place can be any digit from 1 to 9 except K and other digits can have any value between 0 to 9 except K.  
  • Total count of N digit numbers with at least one occurrence of
    = Count of all N digits numbers –  Count of all N digit numbers with no occurrence of K.

Below is the implementation of the above approach:

C++

// C++ Code to Implement the approach
// Function to find the total possible numbers
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the total possible numbers
void required_numbers(int n, int k)
{
    int t, h, r;
   
    // Find all n digits numbers
    t = 9 * pow (10, (n - 1));
   
    // Find n digits number in which no k occurs
    h = 8 * pow (9, (n - 1));
   
    // Calculate the required value as
    // the difference of the above two values
    r = t - h;
    cout << r;
}
 
// Driver code
int main()
{
 
    int N, K;
    N = 3;
    K = 2;
   
    // Function call
    required_numbers(N, K);
    return 0;
}
 
// This code is contributed by ANKITKUMAR34.

Java

// Java Code to implement the approach
// Function to find the total possible numbers
import java.io.*;
import java.util.*;
 
class GFG {
    // Function to find the total possible numbers
    public static void required_numbers(int n, int k)
    {
        // Find all n digits numbers
        int t = 9 * (int)Math.pow(10, (n - 1));
 
        // Find n digits number in which no k occurs
        int h = 8 * (int)Math.pow(9, (n - 1));
 
        // Calculate the required value as
        // the difference of the above two values
        int r = t - h;
        System.out.print(r);
    }
    public static void main(String[] args)
    {
        int N = 3;
        int K = 2;
 
        // Function call
        required_numbers(N, K);
    }
}
 
// This code is contributed by Rohit Pradhan

Python3

# Python Code to Implement the approach
 
 
# Function to find the total possible numbers
def required_numbers(n, k):
 
    # Find all n digits numbers
    t = 9 * 10 ** (n - 1)
 
    # Find n digits number in which no k occurs
    h = 8 * 9 ** (n - 1)
 
    # Calculate the required value as
    # the difference of the above two values
    r = t - h
    return(r)
 
 
# Driver code
if __name__ == '__main__':
    N = 3
    K = 2
 
    # Function call
    print(required_numbers(N, K))

C#

// C# Code to implement the approach
// Function to find the total possible numbers
 
using System;
 
public class GFG {
     
    // Function to find the total possible numbers
    public static void required_numbers(int n, int k)
    {
        // Find all n digits numbers
        int t = 9 * (int)Math.Pow(10, (n - 1));
 
        // Find n digits number in which no k occurs
        int h = 8 * (int)Math.Pow(9, (n - 1));
 
        // Calculate the required value as
        // the difference of the above two values
        int r = t - h;
         
        Console.WriteLine(r);
    }
     
    public static void Main(string[] args)
    {
        int N = 3;
        int K = 2;
 
        // Function call
        required_numbers(N, K);
    }
}
 
// This code is contributed by AnkThon

Javascript

<script>
// javascript Code to implement the approach
// Function to find the total possible numbers
 
   // Function to find the total possible numbers
    function required_numbers(n , k)
    {
        // Find all n digits numbers
        var t = 9 * parseInt(Math.pow(10, (n - 1)));
 
        // Find n digits number in which no k occurs
        var h = 8 * parseInt(Math.pow(9, (n - 1)));
 
        // Calculate the required value as
        // the difference of the above two values
        var r = t - h;
        document.write(r);
    }
     
var N = 3;
var K = 2;
 
// Function call
required_numbers(N, K);
// This code contributed by shikhasingrajput
</script>

Output

252

Time Complexity: O(1).
Auxiliary Space: O(1).




Reffered: https://www.geeksforgeeks.org


Mathematical

Related
Parity of final value after repeated circular removal of Kth Integer Parity of final value after repeated circular removal of Kth Integer
Find minimum and maximum number of terms to divide N as sum of 4 or 6 Find minimum and maximum number of terms to divide N as sum of 4 or 6
Compute nCr % p | Set 4 (Chinese Remainder theorem with Lucas Theorem) Compute nCr % p | Set 4 (Chinese Remainder theorem with Lucas Theorem)
Find the largest co-prime fraction less than the given fraction Find the largest co-prime fraction less than the given fraction
Probability of hitting the target Nth time at Mth throw Probability of hitting the target Nth time at Mth throw

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
16