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.

**Examples:**

Input:N = 2, M = 2

Output:3

The numbers are 1001, 1010 and 1100.

Input:N = 2, M = 3

Output:6

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:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define ll long long int ` ` ` `// Function to return the factorial of a number ` `ll factorial(` `int` `f) ` `{ ` ` ` `ll fact = 1; ` ` ` ` ` `for` `(` `int` `i = 2; i <= f; i++) ` ` ` `fact *= (ll)i; ` ` ` ` ` `return` `fact; ` `} ` ` ` `// Function to return the count of distinct ` `// (N + M) digit numbers having N 0's ` `// and and M 1's with no leading zeros ` `ll findPermutation(` `int` `N, ` `int` `M) ` `{ ` ` ` `ll permutation = factorial(N + M - 1) ` ` ` `/ (factorial(N) * factorial(M - 1)); ` ` ` `return` `permutation; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 3, M = 3; ` ` ` `cout << findPermutation(N, M); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` `// Function to return the factorial of a number ` `static` `int` `factorial(` `int` `f) ` `{ ` ` ` `int` `fact = ` `1` `; ` ` ` ` ` `for` `(` `int` `i = ` `2` `; i <= f; i++) ` ` ` `fact *= (` `int` `)i; ` ` ` ` ` `return` `fact; ` `} ` ` ` `// Function to return the count of distinct ` `// (N + M) digit numbers having N 0's ` `// and and M 1's with no leading zeros ` `static` `int` `findPermutation(` `int` `N, ` `int` `M) ` `{ ` ` ` `int` `permutation = factorial(N + M - ` `1` `) ` ` ` `/ (factorial(N) * factorial(M - ` `1` `)); ` ` ` `return` `permutation; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` ` ` `int` `N = ` `3` `, M = ` `3` `; ` ` ` `System.out.println(findPermutation(N, M)); ` `} ` `} ` ` ` `// This code is contributed ` `// by ajit ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the factorial ` `# of a number ` `def` `factorial(f): ` ` ` `fact ` `=` `1` ` ` `for` `i ` `in` `range` `(` `2` `, f ` `+` `1` `): ` ` ` `fact ` `*` `=` `i ` ` ` `return` `fact ` ` ` `# Function to return the count of distinct ` `# (N + M) digit numbers having N 0's ` `# and and M 1's with no leading zeros ` `def` `findPermuatation(N, M): ` ` ` `permutation ` `=` `(factorial(N ` `+` `M ` `-` `1` `) ` `/` `/` ` ` `(factorial(N) ` `*` `factorial(M ` `-` `1` `))) ` ` ` `return` `permutation ` ` ` `# Driver code ` `N ` `=` `3` `; M ` `=` `3` `print` `(findPermuatation(N, M)) ` ` ` `# This code is contributed ` `# by Shrikant13 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` `// Function to return the factorial of a number ` `static` `int` `factorial(` `int` `f) ` `{ ` ` ` `int` `fact = 1; ` ` ` ` ` `for` `(` `int` `i = 2; i <= f; i++) ` ` ` `fact *= (` `int` `)i; ` ` ` ` ` `return` `fact; ` `} ` ` ` `// Function to return the count of distinct ` `// (N + M) digit numbers having N 0's ` `// and and M 1's with no leading zeros ` `static` `int` `findPermutation(` `int` `N, ` `int` `M) ` `{ ` ` ` `int` `permutation = factorial(N + M - 1) ` ` ` `/ (factorial(N) * factorial(M - 1)); ` ` ` `return` `permutation; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `N = 3, M = 3; ` ` ` `Console.Write(findPermutation(N, M)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Akanksha Rai ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` ` ` `// PHP implementation of the approach ` `// Function to return the factorial of a number ` `function` `factorial(` `$f` `) ` `{ ` ` ` `$fact` `= 1; ` ` ` ` ` `for` `(` `$i` `= 2; ` `$i` `<= ` `$f` `; ` `$i` `++) ` ` ` `$fact` `*= ` `$i` `; ` ` ` ` ` `return` `$fact` `; ` `} ` ` ` `// Function to return the count of distinct ` `// (N + M) digit numbers having N 0's ` `// and and M 1's with no leading zeros ` `function` `findPermutation(` `$N` `,` `$M` `) ` `{ ` ` ` `$permutation` `= factorial(` `$N` `+ ` `$M` `- 1) ` ` ` `/ (factorial(` `$N` `) * factorial(` `$M` `- 1)); ` ` ` `return` `$permutation` `; ` `} ` ` ` ` ` `// Driver code ` ` ` `$N` `= 3; ` ` ` `$M` `= 3; ` ` ` `echo` `findPermutation(` `$N` `, ` `$M` `); ` ` ` `// This code is contributed by ajit.. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

10

## Recommended Posts:

- Count of N-bit binary numbers without leading zeros
- All possible numbers of N digits and base B without leading zeros
- Remove leading zeros from a Number given as a string
- Check if any permutation of a number without any leading zeros is a power of 2 or not
- Count ways to split a Binary String into three substrings having equal count of zeros
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count unique numbers that can be generated from N by adding one and removing trailing zeros
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Numbers whose factorials end with n zeros
- Count Pairs Of Consecutive Zeros
- Count number of trailing zeros in product of array
- Count binary strings with twice zeros in first half
- Count number of trailing zeros in (1^1)*(2^2)*(3^3)*(4^4)*..
- Count number of trailing zeros in Binary representation of a number using Bitset
- Golang Program to Count Trailing Zeros in Factorial of a Number
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Count of Numbers in a Range divisible by m and having digit d in even positions
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max 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 contribute@geeksforgeeks.org. 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.