Count number of binary strings of length N having only 0’s and 1’s
Last Updated :
09 Jun, 2022
Given an integer N, the task is to count the number of binary strings of length N having only 0’s and 1’s.
Note: Since the count can be very large, return the answer modulo 10^9+7.
Examples:
Input: 2
Output: 4
Explanation: The numbers are 00, 01, 11, 10. Hence the count is 4.
Input: 3
Output: 8
Explanation: The numbers are 000, 001, 011, 010, 111, 101, 110, 100. Hence the count is 8.
Approach: The problem can be easily solved by using Permutation and Combination. At each position of the string there can only be two possibilities, i.e., 0 or 1. Therefore, the total number of permutation of 0 and 1 in a string of length N is given by 2*2*2*…(N times), i.e., 2^N. The answer can be very large, hence modulo by 10^9+7 is returned.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod (ll)(1e9 + 7)
ll power(ll x, ll y, ll p)
{
ll res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
ll findCount(ll N)
{
int count = power(2, N, mod);
return count;
}
int main()
{
ll N = 25;
cout << findCount(N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int mod = ( int ) (1e9 + 7 );
static int power( int x, int y, int p)
{
int res = 1 ;
x = x % p;
while (y > 0 )
{
if ((y & 1 )== 1 )
res = (res * x) % p;
y = y >> 1 ;
x = (x * x) % p;
}
return res;
}
static int findCount( int N)
{
int count = power( 2 , N, mod);
return count;
}
public static void main(String[] args)
{
int N = 25 ;
System.out.println(findCount(N));
}
}
|
Python3
mod = 1000000007
def power(x, y, p):
res = 1
x = x % p
while (y > 0 ):
if (y & 1 ):
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
def findCount(N):
count = power( 2 , N, mod)
return count
if __name__ = = '__main__' :
N = 25
print (findCount(N))
|
C#
using System;
class GFG
{
static int mod = ( int ) (1e9 + 7);
static int power( int x, int y, int p)
{
int res = 1;
x = x % p;
while (y > 0)
{
if ((y & 1) == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static int findCount( int N)
{
int count = power(2, N, mod);
return count;
}
public static void Main()
{
int N = 25;
Console.WriteLine(findCount(N));
}
}
|
PHP
<?php
function power( $x , $y )
{
$p = 1000000007;
$res = 1;
$x = $x % $p ;
while ( $y > 0)
{
if ( $y & 1)
$res = ( $res * $x ) % $p ;
$y = $y >> 1;
$x = ( $x * $x ) % $p ;
}
return $res ;
}
function findCount( $N )
{
$count = power(2, $N );
return $count ;
}
$N = 25;
echo findCount( $N );
?>
|
Javascript
<script>
mod = 1000000007
function power(x, y, p)
{
var res = 1;
x = x % p;
while (y > 0)
{
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
function findCount(N)
{
var count = power(2, N, mod);
return count;
}
var N = 25;
document.write(findCount(N));
</script>
|
Time Complexity: O(logn)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...