Count numbers having N 0’s and M 1’s with no leading zeros
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 the 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++
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll factorial( int f)
{
ll fact = 1;
for ( int i = 2; i <= f; i++)
fact *= (ll)i;
return fact;
}
ll findPermutation( int N, int M)
{
ll permutation = factorial(N + M - 1)
/ (factorial(N) * factorial(M - 1));
return permutation;
}
int main()
{
int N = 3, M = 3;
cout << findPermutation(N, M);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int factorial( int f)
{
int fact = 1 ;
for ( int i = 2 ; i <= f; i++)
fact *= ( int )i;
return fact;
}
static int findPermutation( int N, int M)
{
int permutation = factorial(N + M - 1 )
/ (factorial(N) * factorial(M - 1 ));
return permutation;
}
public static void main (String[] args)
{
int N = 3 , M = 3 ;
System.out.println(findPermutation(N, M));
}
}
|
Python3
def factorial(f):
fact = 1
for i in range ( 2 , f + 1 ):
fact * = i
return fact
def findPermuatation(N, M):
permutation = (factorial(N + M - 1 ) / /
(factorial(N) * factorial(M - 1 )))
return permutation
N = 3 ; M = 3
print (findPermuatation(N, M))
|
C#
using System;
class GFG
{
static int factorial( int f)
{
int fact = 1;
for ( int i = 2; i <= f; i++)
fact *= ( int )i;
return fact;
}
static int findPermutation( int N, int M)
{
int permutation = factorial(N + M - 1)
/ (factorial(N) * factorial(M - 1));
return permutation;
}
public static void Main()
{
int N = 3, M = 3;
Console.Write(findPermutation(N, M));
}
}
|
PHP
<?php
function factorial( $f )
{
$fact = 1;
for ( $i = 2; $i <= $f ; $i ++)
$fact *= $i ;
return $fact ;
}
function findPermutation( $N , $M )
{
$permutation = factorial( $N + $M - 1)
/ (factorial( $N ) * factorial( $M - 1));
return $permutation ;
}
$N = 3;
$M = 3;
echo findPermutation( $N , $M );
?>
|
Javascript
<script>
function factorial(f)
{
var fact = 1;
for ( var i = 2; i <= f; i++)
fact *= i;
return fact;
}
function findPermutation(N, M)
{
var permutation = factorial(N + M - 1) /
(factorial(N) * factorial(M - 1));
return permutation;
}
var N = 3, M = 3;
document.write(findPermutation(N, M));
</script>
|
Time Complexity: O(N+M)
Auxiliary Space: O(1), as no extra space is used
Last Updated :
14 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...