Count of N digit numbers possible which satisfy the given conditions
Given an integer N, the Task is to find the total number of N digit numbers possible such that:
- All the digits of the numbers are from the range [0, N].
- There are no leading 0s.
- All the digits in a number are distinct.
Input: N = 2
10, 12, 20 and 21 are the only possible 2 digit
numbers which satisfy the given conditions.
Input: N = 5
Approach: Given N number of digit and the first place can be filled in N ways [0 cannot be taken as the first digit and the allowed digits are from the range [1, N]]
Remaining (N – 1) places can be filled in N! ways
So, total count of number possible will be N * N!.
Take an example for better understanding. Say, N = 8
First place can be filled with any digit from [1, 8] and the remaining 7 places can be filled in 8! ways i.e 8 * 7 * 6 * 5 * 4 * 3 * 2.
So, total ways = 8 * 8! = 8 * 8 * 7 * 6 * 5 * 4 * 3 * 2 = 322560
Below is the implementation of the above approach:
Time Complexity: O(n)
Auxiliary Space: O(1)