Count of N-digit numbers with all distinct digits
Last Updated :
13 Mar, 2023
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 which will be repeating hence for such cases the answer will be 0 else for the values of N = 1, 2, 3, ..., 9, a series will be formed as 9, 81, 648, 4536, 27216, 136080, 544320, ... whose Nth term will be 9 * 9! / (10 - N)!.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the factorial of n
int factorial(int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
// Function to return the count
// of n-digit numbers with
// all distinct digits
int countNum(int n)
{
if (n > 10)
return 0;
return (9 * factorial(9)
/ factorial(10 - n));
}
// Driver code
int main()
{
int n = 3;
cout << countNum(n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the factorial of n
static int factorial(int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
// Function to return the count
// of n-digit numbers with
// all distinct digits
static int countNum(int n)
{
if (n > 10)
return 0;
return (9 * factorial(9) /
factorial(10 - n));
}
// Driver code
public static void main(String []args)
{
int n = 3;
System.out.println(countNum(n));
}
}
// This code is contributed by Srathore
Python3
# Python3 implementation of the approach
# Function to return the factorial of n
def factorial(n) :
if (n == 0) :
return 1;
return n * factorial(n - 1);
# Function to return the count
# of n-digit numbers with
# all distinct digits
def countNum(n) :
if (n > 10) :
return 0;
return (9 * factorial(9) //
factorial(10 - n));
# Driver code
if __name__ == "__main__" :
n = 3;
print(countNum(n));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the factorial of n
static int factorial(int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
// Function to return the count
// of n-digit numbers with
// all distinct digits
static int countNum(int n)
{
if (n > 10)
return 0;
return (9 * factorial(9) /
factorial(10 - n));
}
// Driver code
public static void Main(String []args)
{
int n = 3;
Console.WriteLine(countNum(n));
}
}
// This code is contributed by Princi Singh
JavaScript
<script>
// Javascript implementation of the approach
// Function to return the factorial of n
function factorial(n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
// Function to return the count
// of n-digit numbers with
// all distinct digits
function countNum(n)
{
if (n > 10)
return 0;
return (9 * factorial(9)
/ factorial(10 - n));
}
// Driver code
var n = 3;
document.write(countNum(n));
// This code is contributed by rutvik_56.
</script>
Time Complexity: O(n)
Auxiliary Space: O(n)
Efficient Approach: We have to fill (n) places with different digit. Like we n=2 then (_ _) places to fill. first place we fill (1 to 9) any number. let we fill 9 in first place then in second place we have choice (0 to 8). So for first place we 9 choices because we can not fill 0 at first place and after that for 2nd place we 9 choice and for 3rd place we 8 choice then so on...
let we take 4 digit no so ( 9choice 9 choice 8 choice 7choice) .
Choices->
first place -9
second place-9
third place -8
fourth place -7
and so on.......
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the count
// of n-digit numbers with
// all distinct digits
long long countNum(int n)
{
if (n > 10)
return 0;
long long count = 1; // Store the count
long long j = 9; // take choice
/* take loop 1 to n and multiply with choice*/
for (int i = 1; i <= n; i++) {
if (i == 1) {
count = count * j;
continue;
}
else {
count = count * j;
j--;
}
}
return count;
}
// Driver code
int main()
{
int n = 3;
cout << countNum(n);
return 0;
}
Java
// Java implementation of the approach
class GFG {
// Function to return the count
// of n-digit numbers with
// all distinct digits
static long countNum(int n)
{
if (n > 10)
return 0;
long count = 1; // Store the count
long j = 9; // take choice
/* take loop 1 to n and multiply with choice*/
for (int i = 1; i <= n; i++) {
if (i == 1) {
count = count * j;
continue;
}
else {
count = count * j;
j--;
}
}
return count;
}
// Driver code
public static void main(String[] args)
{
int n = 3;
System.out.println(countNum(n));
}
}
// This code is contributed by Srathore
Python3
# Python implementation of the approach
# Function to return the count
# of n-digit numbers with
# all distinct digits
def countNum(n):
if n > 10:
return 0
count = 1 # Store the count
j = 9 # take choice
# take loop 1 to n and multiply with choice
for i in range(1, n+1):
if i == 1:
count = count * j
continue
else:
count = count * j
j -= 1
return count
# Driver code
n = 3
print(countNum(n))
# This code is contributed by rutikbhosale
C#
using System;
class GFG
{
// Function to return the count
// of n-digit numbers with
// all distinct digits
static long countNum(int n)
{
if (n > 10)
return 0;
long count = 1; // Store the count
long j = 9; // take choice
/* take loop 1 to n and multiply with choice*/
for (int i = 1; i <= n; i++)
{
if (i == 1)
{
count = count * j;
continue;
}
else
{
count = count * j;
j--;
}
}
return count;
}
// Driver code
static void Main(string[] args)
{
int n = 3;
Console.WriteLine(countNum(n));
}
}
JavaScript
// Javascript implementation of the approach
// Function to return the count
// of n-digit numbers with
// all distinct digits
function countNum(n) {
if (n > 10)
return 0;
let count = 1; // Store the count
let j = 9; // take choice
/* take loop 1 to n and multiply with choice*/
for (let i = 1; i <= n; i++) {
if (i === 1) {
count *= j;
continue;
} else {
count *= j;
j--;
}
}
return count;
}
// Driver code
const n = 3;
console.log(countNum(n));
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
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 = 2Output: 252Explanation: 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 value
6 min read
Count numbers with exactly K non-zero digits and distinct odd digit sum Given an Integer N, and a number K, the task is to find out the total numbers from 0 to N which have exactly K non zero digits and the sum of those digits should be odd and that sum should be distinct. The number N can be as large as 10^18.Examples: Input : N = 10, K = 1 Output : 5 The numbers which
14 min read
Count numbers with exactly K non-zero digits and distinct odd digit sum Given an Integer N, and a number K, the task is to find out the total numbers from 0 to N which have exactly K non zero digits and the sum of those digits should be odd and that sum should be distinct. The number N can be as large as 10^18.Examples: Input : N = 10, K = 1 Output : 5 The numbers which
14 min read
Count numbers with exactly K non-zero digits and distinct odd digit sum Given an Integer N, and a number K, the task is to find out the total numbers from 0 to N which have exactly K non zero digits and the sum of those digits should be odd and that sum should be distinct. The number N can be as large as 10^18.Examples: Input : N = 10, K = 1 Output : 5 The numbers which
14 min read
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 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