Count of N digit numbers with at least one digit as K
Last Updated :
03 Aug, 2022
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++
// 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>
Time Complexity: O(1).
Auxiliary Space: O(1).
Similar Reads
Count of N-digit numbers with at least one digit repeating Given a positive integer n, the task is to find the count of n-digit numbers with at least one repeated digit.Examples:Input: n = 2Output: 9Explanation: All the 2-digit numbers with at least one repeated digit are 11, 22, 33, 44, 55, 66, 77, 88, 99. Therefore, the total count is 9.Input: n = 5Output
15+ min read
Count of N-digit numbers with all distinct digits Given an integer N, the task is to find the count of N-digit numbers with all distinct digits.Examples: Input: N = 1 Output: 9 1, 2, 3, 4, 5, 6, 7, 8 and 9 are the 1-digit numbers with all distinct digits.Input: N = 3 Output: 648 Naive Approach: If N > 10 i.e. there will be atleast one digit whic
7 min read
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 num
14 min read
Count of N-digit numbers whose bitwise AND of adjacent digits equals 0 Given a positive integer N, the task is to count the number of N-digit numbers such that the bitwise AND of adjacent digits equals 0. Examples: Input: N = 1Output: 10Explanation: All numbers from 0 to 9 satisfy the given condition as there is only one digit. Input: N = 3Output: 264 Naive Approach: T
8 min read
Count of N-digit numbers in base K with no two consecutive zeroes Given two integers N and K, the task is to find the count of all the integer in base K which satisfy the following conditions: The integers must be of exactly N digits.There should be no leading 0.There must not be any consecutive digit pair such that both the digits are 0.Examples: Input: N = 3, K
15+ min read
Count of N-digit numbers in base K with no two consecutive zeroes Given two integers N and K, the task is to find the count of all the integer in base K which satisfy the following conditions: The integers must be of exactly N digits.There should be no leading 0.There must not be any consecutive digit pair such that both the digits are 0.Examples: Input: N = 3, K
15+ min read