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
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 nint 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 digitsint countNum(int n){ if (n > 10) return 0; return (9 * factorial(9) / factorial(10 - n));}// Driver codeint main(){ int n = 3; cout << countNum(n); return 0;} |
Java
// Java implementation of the approachclass 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 ndef factorial(n) : if (n == 0) : return 1; return n * factorial(n - 1);# Function to return the count# of n-digit numbers with# all distinct digitsdef countNum(n) : if (n > 10) : return 0; return (9 * factorial(9) // factorial(10 - n));# Driver codeif __name__ == "__main__" : n = 3; print(countNum(n)); # This code is contributed by AnkitRai01 |
C#
// C# implementation of the approachusing 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 nfunction factorial(n){ if (n == 0) return 1; return n * factorial(n - 1);}// Function to return the count// of n-digit numbers with// all distinct digitsfunction countNum(n){ if (n > 10) return 0; return (9 * factorial(9) / factorial(10 - n));}// Driver codevar n = 3;document.write(countNum(n));// This code is contributed by rutvik_56.</script> |
648
Time Complexity: O(n)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


