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 K
= 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++
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void required_numbers( int n, int k)
{
int t, h, r;
t = 9 * pow (10, (n - 1));
h = 8 * pow (9, (n - 1));
r = t - h;
cout << r;
}
int main()
{
int N, K;
N = 3;
K = 2;
required_numbers(N, K);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static void required_numbers( int n, int k)
{
int t = 9 * ( int )Math.pow( 10 , (n - 1 ));
int h = 8 * ( int )Math.pow( 9 , (n - 1 ));
int r = t - h;
System.out.print(r);
}
public static void main(String[] args)
{
int N = 3 ;
int K = 2 ;
required_numbers(N, K);
}
}
|
Python3
def required_numbers(n, k):
t = 9 * 10 * * (n - 1 )
h = 8 * 9 * * (n - 1 )
r = t - h
return (r)
if __name__ = = '__main__' :
N = 3
K = 2
print (required_numbers(N, K))
|
C#
using System;
public class GFG {
public static void required_numbers( int n, int k)
{
int t = 9 * ( int )Math.Pow(10, (n - 1));
int h = 8 * ( int )Math.Pow(9, (n - 1));
int r = t - h;
Console.WriteLine(r);
}
public static void Main( string [] args)
{
int N = 3;
int K = 2;
required_numbers(N, K);
}
}
|
Javascript
<script>
function required_numbers(n , k)
{
var t = 9 * parseInt(Math.pow(10, (n - 1)));
var h = 8 * parseInt(Math.pow(9, (n - 1)));
var r = t - h;
document.write(r);
}
var N = 3;
var K = 2;
required_numbers(N, K);
</script>
|
Time Complexity: O(1).
Auxiliary Space: O(1).
Last Updated :
03 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...