Given two integer N and M, the task is to find the number of distinct numbers having N 0’s and M 1’s with no leading zeros and N + M total digits.
Input: N = 2, M = 2
The numbers are 1001, 1010 and 1100.
Input: N = 2, M = 3
The numbers are 10011, 10101, 10110, 11001, 11010 and 11100.
Approach: The problem can be easily solved by finding the total permutation of N similar items and M – 1 similar items. Since no leading zeros are allowed, one 1 is always fixed at the start of the number. The remaining M – 1 1’s and N 0’s are arranged in different permutations. Now, the number of permutations of (A + B) objects where A objects of same type and B objects of other type is given by (A + B)! / (A! * B!). Therefore, the number of distinct numbers is given by (N + M -1)! / (N! * (M – 1)!).
Below is the implementation of the above approach:
- All possible numbers of N digits and base B without leading zeros
- Count unique numbers that can be generated from N by adding one and removing trailing zeros
- Count Pairs Of Consecutive Zeros
- Count number of trailing zeros in (1^1)*(2^2)*(3^3)*(4^4)*..
- Numbers whose factorials end with n zeros
- Count binary strings with twice zeros in first half
- Count number of trailing zeros in product of array
- Count number of trailing zeros in Binary representation of a number using Bitset
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Count numbers which can be constructed using two numbers
- Count numbers which are divisible by all the numbers from 2 to 10
- Count numbers that don't contain 3
- Count numbers with same first and last digits
- Count Odd and Even numbers in a range from L to R
- Count of numbers satisfying m + sum(m) + sum(sum(m)) = N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.