Number of Binary Strings of length N with K adjacent Set Bits
Last Updated :
05 May, 2025
Given n and k . The task is to find the number of binary strings of length n out of 2n such that they satisfy f(bit string) = k.
Where,
f(x) = Number of times a set bit is adjacent to
another set bit in a binary string x.
For Example:
f(011101101) = 3
f(010100000) = 0
f(111111111) = 8
Examples:
Input : n = 5, k = 2
Output : 6
Explanation
There are 6 ways to form bit strings of length 5
such that f(bit string s) = 2,
These possible strings are:-
00111, 01110, 10111, 11011, 11100, 11101
Method 1 (Brute Force):
The simplest approach is to solve the problem recursively, by passing the current index, the value of f(bit string) formed till current index and the last bit we placed in the binary string formed till (current index - 1). If we reach the index where current index = n and the value of f(bit string) = k then we count this way else we dont.
Below is the implementation of the brute force approach:
C++
// C++ program to find the number of Bit Strings
// of length N with K adjacent set bits
#include <bits/stdc++.h>
using namespace std;
// Function to find the number of Bit Strings
// of length N with K adjacent set bits
int waysToKAdjacentSetBits(int n, int k, int currentIndex,
int adjacentSetBits, int lastBit)
{
/* Base Case when we form bit string of length n */
if (currentIndex == n) {
// if f(bit string) = k, count this way
if (adjacentSetBits == k)
return 1;
return 0;
}
int noOfWays = 0;
/* Check if the last bit was set,
if it was set then call for
next index by incrementing the
adjacent bit count else just call
the next index with same value of
adjacent bit count and either set the
bit at current index or let it remain
unset */
if (lastBit == 1) {
// set the bit at currentIndex
noOfWays += waysToKAdjacentSetBits(n, k, currentIndex + 1,
adjacentSetBits + 1, 1);
// unset the bit at currentIndex
noOfWays += waysToKAdjacentSetBits(n, k,currentIndex + 1,
adjacentSetBits, 0);
}
else if (!lastBit) {
noOfWays += waysToKAdjacentSetBits(n, k, currentIndex + 1,
adjacentSetBits, 1);
noOfWays += waysToKAdjacentSetBits(n, k, currentIndex + 1,
adjacentSetBits, 0);
}
return noOfWays;
}
// Driver Code
int main()
{
int n = 5, k = 2;
/* total ways = (ways by placing 1st bit as 1 +
ways by placing 1st bit as 0) */
int totalWays = waysToKAdjacentSetBits(n, k, 1, 0, 1)
+ waysToKAdjacentSetBits(n, k, 1, 0, 0);
cout << "Number of ways = " << totalWays << "\n";
return 0;
}
Java
// Java program to find the number of Bit Strings
// of length N with K adjacent set bits
import java.util.*;
class solution
{
// Function to find the number of Bit Strings
// of length N with K adjacent set bits
static int waysToKAdjacentSetBits(int n, int k, int currentIndex,
int adjacentSetBits, int lastBit)
{
// Base Case when we form bit string of length n
if (currentIndex == n) {
// if f(bit string) = k, count this way
if (adjacentSetBits == k)
return 1;
return 0;
}
int noOfWays = 0;
/* Check if the last bit was set,
if it was set then call for
next index by incrementing the
adjacent bit count else just call
the next index with same value of
adjacent bit count and either set the
bit at current index or let it remain
unset */
if (lastBit == 1) {
// set the bit at currentIndex
noOfWays += waysToKAdjacentSetBits(n, k, currentIndex + 1,
adjacentSetBits + 1, 1);
// unset the bit at currentIndex
noOfWays += waysToKAdjacentSetBits(n, k,currentIndex + 1,
adjacentSetBits, 0);
}
else if (lastBit == 0) {
noOfWays += waysToKAdjacentSetBits(n, k, currentIndex + 1,
adjacentSetBits, 1);
noOfWays += waysToKAdjacentSetBits(n, k, currentIndex + 1,
adjacentSetBits, 0);
}
return noOfWays;
}
// Driver Code
public static void main(String args[])
{
int n = 5, k = 2;
/* total ways = (ways by placing 1st bit as 1 +
ways by placing 1st bit as 0) */
int totalWays = waysToKAdjacentSetBits(n, k, 1, 0, 1)
+ waysToKAdjacentSetBits(n, k, 1, 0, 0);
System.out.println("Number of ways = "+totalWays);
}
}
//This code is contributed by
// Surendra _Gangwar
Python
# Python 3 program to find the number of Bit
# Strings of length N with K adjacent set bits
# Function to find the number of Bit Strings
# of length N with K adjacent set bits
def waysToKAdjacentSetBits(n, k, currentIndex,
adjacentSetBits, lastBit):
# Base Case when we form bit string of length n
if (currentIndex == n):
# if f(bit string) = k, count this way
if (adjacentSetBits == k):
return 1;
return 0
noOfWays = 0
# Check if the last bit was set, if it was set
# then call for next index by incrementing the
# adjacent bit count else just call the next
# index with same value of adjacent bit count
# and either set the bit at current index or
# let it remain unset
if (lastBit == 1):
# set the bit at currentIndex
noOfWays += waysToKAdjacentSetBits(n, k, currentIndex + 1,
adjacentSetBits + 1, 1);
# unset the bit at currentIndex
noOfWays += waysToKAdjacentSetBits(n, k,currentIndex + 1,
adjacentSetBits, 0);
elif (lastBit != 1):
noOfWays += waysToKAdjacentSetBits(n, k, currentIndex + 1,
adjacentSetBits, 1);
noOfWays += waysToKAdjacentSetBits(n, k, currentIndex + 1,
adjacentSetBits, 0);
return noOfWays;
# Driver Code
n = 5; k = 2;
# total ways = (ways by placing 1st bit as 1 +
# ways by placing 1st bit as 0)
totalWays = (waysToKAdjacentSetBits(n, k, 1, 0, 1) +
waysToKAdjacentSetBits(n, k, 1, 0, 0));
print("Number of ways =", totalWays);
# This code is contributed by Akanksha Rai
C#
// C# program to find the number of Bit Strings
// of length N with K adjacent set bits
using System;
class GFG
{
// Function to find the number of Bit Strings
// of length N with K adjacent set bits
static int waysToKAdjacentSetBits(int n, int k, int currentIndex,
int adjacentSetBits, int lastBit)
{
/* Base Case when we form bit
string of length n */
if (currentIndex == n)
{
// if f(bit string) = k, count this way
if (adjacentSetBits == k)
return 1;
return 0;
}
int noOfWays = 0;
/* Check if the last bit was set, if it was
set then call for next index by incrementing
the adjacent bit count else just call the next
index with same value of adjacent bit count and
either set the bit at current index or let it
remain unset */
if (lastBit == 1)
{
// set the bit at currentIndex
noOfWays += waysToKAdjacentSetBits(n, k, currentIndex + 1,
adjacentSetBits + 1, 1);
// unset the bit at currentIndex
noOfWays += waysToKAdjacentSetBits(n, k,currentIndex + 1,
adjacentSetBits, 0);
}
else if (lastBit != 1)
{
noOfWays += waysToKAdjacentSetBits(n, k, currentIndex + 1,
adjacentSetBits, 1);
noOfWays += waysToKAdjacentSetBits(n, k, currentIndex + 1,
adjacentSetBits, 0);
}
return noOfWays;
}
// Driver Code
public static void Main()
{
int n = 5, k = 2;
/* total ways = (ways by placing 1st bit as 1 +
ways by placing 1st bit as 0) */
int totalWays = waysToKAdjacentSetBits(n, k, 1, 0, 1) +
waysToKAdjacentSetBits(n, k, 1, 0, 0);
Console.WriteLine("Number of ways = " + totalWays);
}
}
// This code is contributed
// by Akanksha Rai
JavaScript
<script>
// Javascript program to find the number of Bit Strings
// of length N with K adjacent set bits
// Function to find the number of Bit Strings
// of length N with K adjacent set bits
function waysToKAdjacentSetBits(n, k, currentIndex,
adjacentSetBits, lastBit)
{
/* Base Case when we form bit string of length n */
if (currentIndex == n) {
// if f(bit string) = k, count this way
if (adjacentSetBits == k)
return 1;
return 0;
}
let noOfWays = 0;
/* Check if the last bit was set,
if it was set then call for
next index by incrementing the
adjacent bit count else just call
the next index with same value of
adjacent bit count and either set the
bit at current index or let it remain
unset */
if (lastBit == 1) {
// set the bit at currentIndex
noOfWays += waysToKAdjacentSetBits(n, k, currentIndex + 1,
adjacentSetBits + 1, 1);
// unset the bit at currentIndex
noOfWays += waysToKAdjacentSetBits(n, k,currentIndex + 1,
adjacentSetBits, 0);
}
else if (!lastBit) {
noOfWays += waysToKAdjacentSetBits(n, k, currentIndex + 1,
adjacentSetBits, 1);
noOfWays += waysToKAdjacentSetBits(n, k, currentIndex + 1,
adjacentSetBits, 0);
}
return noOfWays;
}
// Driver Code
let n = 5, k = 2;
/* total ways = (ways by placing 1st bit as 1 +
ways by placing 1st bit as 0) */
let totalWays = waysToKAdjacentSetBits(n, k, 1, 0, 1)
+ waysToKAdjacentSetBits(n, k, 1, 0, 0);
document.write("Number of ways = " + totalWays);
</script>
PHP
<?php
// PHP program to find the number of
// Bit Strings of length N with K
// adjacent set bits
// Function to find the number of Bit Strings
// of length N with K adjacent set bits
function waysToKAdjacentSetBits($n, $k, $currentIndex,
$adjacentSetBits, $lastBit)
{
/* Base Case when we form bit
string of length n */
if ($currentIndex == $n)
{
// if f(bit string) = k, count this way
if ($adjacentSetBits == $k)
return 1;
return 0;
}
$noOfWays = 0;
/* Check if the last bit was set, if it
was set then call for next index by
incrementing the adjacent bit count else
just call the next index with same value
of adjacent bit count and either set the
bit at current index or let it remain
unset */
if ($lastBit == 1)
{
// set the bit at currentIndex
$noOfWays += waysToKAdjacentSetBits($n, $k, $currentIndex + 1,
$adjacentSetBits + 1, 1);
// unset the bit at currentIndex
$noOfWays += waysToKAdjacentSetBits($n, $k,$currentIndex + 1,
$adjacentSetBits, 0);
}
else if (!$lastBit)
{
$noOfWays += waysToKAdjacentSetBits($n, $k, $currentIndex + 1,
$adjacentSetBits, 1);
$noOfWays += waysToKAdjacentSetBits($n, $k, $currentIndex + 1,
$adjacentSetBits, 0);
}
return $noOfWays;
}
// Driver Code
$n = 5;
$k = 2;
/* total ways = (ways by placing 1st bit as 1 +
ways by placing 1st bit as 0) */
$totalWays = waysToKAdjacentSetBits($n, $k, 1, 0, 1) +
waysToKAdjacentSetBits($n, $k, 1, 0, 0);
echo "Number of ways = ", $totalWays, "\n";
// This code is contributed by ajit
?>
Method 2 (efficient): In method 1, there are overlapping subproblems to remove for which we can apply Dynamic Programming (Memoization).
To optimize method 1, we can apply memoization to the above recursive solution such that,
DP[i][j][k] = Number of ways to form bit string of length i with
f(bit string till i) = j where the last bit is k,
which can be 0 or 1 depending on whether the
last bit was set or not
Below is the implementation of the efficient approach:
C++
// C++ program to find the number of Bit Strings
// of length N with K adjacent set bits
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000
// Function to find the number of Bit Strings
// of length N with K adjacent set bits
int waysToKAdjacentSetBits(int dp[][MAX][2], int n, int k,
int currentIndex, int adjacentSetBits, int lastBit)
{
/* Base Case when we form bit
string of length n */
if (currentIndex == n) {
// if f(bit string) = k, count this way
if (adjacentSetBits == k)
return 1;
return 0;
}
if (dp[currentIndex][adjacentSetBits][lastBit] != -1) {
return dp[currentIndex][adjacentSetBits][lastBit];
}
int noOfWays = 0;
/* Check if the last bit was set,
if it was set then call for
next index by incrementing the
adjacent bit count else just call
the next index with same value of
adjacent bit count and either set the
bit at current index or let it remain
unset */
if (lastBit == 1) {
// set the bit at currentIndex
noOfWays += waysToKAdjacentSetBits(dp, n, k, currentIndex + 1,
adjacentSetBits + 1, 1);
// unset the bit at currentIndex
noOfWays += waysToKAdjacentSetBits(dp, n, k, currentIndex + 1,
adjacentSetBits, 0);
}
else if (!lastBit) {
noOfWays += waysToKAdjacentSetBits(dp, n, k, currentIndex + 1,
adjacentSetBits, 1);
noOfWays += waysToKAdjacentSetBits(dp, n, k, currentIndex + 1,
adjacentSetBits, 0);
}
dp[currentIndex][adjacentSetBits][lastBit] = noOfWays;
return noOfWays;
}
// Driver Code
int main()
{
int n = 5, k = 2;
/* dp[i][j][k] represents bit strings of length i
with f(bit string) = j and last bit as k */
int dp[MAX][MAX][2];
memset(dp, -1, sizeof(dp));
/* total ways = (ways by placing 1st bit as 1 +
ways by placing 1st bit as 0) */
int totalWays = waysToKAdjacentSetBits(dp, n, k, 1, 0, 1)
+ waysToKAdjacentSetBits(dp, n, k, 1, 0, 0);
cout << "Number of ways = " << totalWays << "\n";
return 0;
}
Java
// Java program to find the number of Bit Strings
// of length N with K adjacent set bits
class solution
{
static final int MAX=1000;
// Function to find the number of Bit Strings
// of length N with K adjacent set bits
static int waysToKAdjacentSetBits(int dp[][][], int n, int k,
int currentIndex, int adjacentSetBits, int lastBit)
{
/* Base Case when we form bit
string of length n */
if (currentIndex == n) {
// if f(bit string) = k, count this way
if (adjacentSetBits == k)
return 1;
return 0;
}
if (dp[currentIndex][adjacentSetBits][lastBit] != -1) {
return dp[currentIndex][adjacentSetBits][lastBit];
}
int noOfWays = 0;
/* Check if the last bit was set,
if it was set then call for
next index by incrementing the
adjacent bit count else just call
the next index with same value of
adjacent bit count and either set the
bit at current index or let it remain
unset */
if (lastBit == 1) {
// set the bit at currentIndex
noOfWays += waysToKAdjacentSetBits(dp, n, k, currentIndex + 1,
adjacentSetBits + 1, 1);
// unset the bit at currentIndex
noOfWays += waysToKAdjacentSetBits(dp, n, k, currentIndex + 1,
adjacentSetBits, 0);
}
else if (lastBit==0) {
noOfWays += waysToKAdjacentSetBits(dp, n, k, currentIndex + 1,
adjacentSetBits, 1);
noOfWays += waysToKAdjacentSetBits(dp, n, k, currentIndex + 1,
adjacentSetBits, 0);
}
dp[currentIndex][adjacentSetBits][lastBit] = noOfWays;
return noOfWays;
}
// Driver Code
public static void main(String args[])
{
int n = 5, k = 2;
/* dp[i][j][k] represents bit strings of length i
with f(bit string) = j and last bit as k */
int dp[][][]= new int[MAX][MAX][2];
//initialize the dp
for(int i=0;i<MAX;i++)
for(int j=0;j<MAX;j++)
for(int k1=0;k1<2;k1++)
dp[i][j][k1]=-1;
/* total ways = (ways by placing 1st bit as 1 +
ways by placing 1st bit as 0) */
int totalWays = waysToKAdjacentSetBits(dp, n, k, 1, 0, 1)
+ waysToKAdjacentSetBits(dp, n, k, 1, 0, 0);
System.out.print( "Number of ways = " + totalWays + "\n");
}
}
Python
# Python3 program to find the number of Bit Strings
# of length N with K adjacent set bits
MAX = 1000
# Function to find the number of Bit Strings
# of length N with K adjacent set bits
def waysToKAdjacentSetBits(dp, n, k, currentIndex, adjacentSetBits, lastBit):
""" Base Case when we form bit
string of length n """
if currentIndex == n:
# if f(bit string) = k, count this way
if adjacentSetBits == k:
return 1
return 0
if dp[currentIndex][adjacentSetBits][lastBit] != -1:
return dp[currentIndex][adjacentSetBits][lastBit]
noOfWays = 0
""" Check if the last bit was set,
if it was set then call for
next index by incrementing the
adjacent bit count else just call
the next index with same value of
adjacent bit count and either set the
bit at current index or let it remain
unset """
if lastBit == 1:
# set the bit at currentIndex
noOfWays += waysToKAdjacentSetBits(dp, n, k, currentIndex + 1, adjacentSetBits + 1, 1)
# unset the bit at currentIndex
noOfWays += waysToKAdjacentSetBits(dp, n, k, currentIndex + 1, adjacentSetBits, 0)
elif not lastBit:
noOfWays += waysToKAdjacentSetBits(dp, n, k, currentIndex + 1, adjacentSetBits, 1)
noOfWays += waysToKAdjacentSetBits(dp, n, k, currentIndex + 1, adjacentSetBits, 0)
dp[currentIndex][adjacentSetBits][lastBit] = noOfWays
return noOfWays
n, k = 5, 2
""" dp[i][j][k] represents bit strings of length i
with f(bit string) = j and last bit as k """
dp = [[[-1 for i in range(2)] for i in range(MAX)] for j in range(MAX)]
""" total ways = (ways by placing 1st bit as 1 +
ways by placing 1st bit as 0) """
totalWays = waysToKAdjacentSetBits(dp, n, k, 1, 0, 1) + waysToKAdjacentSetBits(dp, n, k, 1, 0, 0)
print( "Number of ways =", totalWays)
# This code is contributed by decode2207.
C#
using System;
// C# program to find the number
// of Bit Strings of length N
// with K adjacent set bits
class GFG
{
static readonly int MAX=1000;
// Function to find the number
// of Bit Strings of length N
// with K adjacent set bits
static int waysToKAdjacentSetBits(int [,,]dp,
int n, int k,
int currentIndex,
int adjacentSetBits,
int lastBit)
{
/* Base Case when we form bit
string of length n */
if (currentIndex == n)
{
// if f(bit string) = k, count this way
if (adjacentSetBits == k)
return 1;
return 0;
}
if (dp[currentIndex, adjacentSetBits,
lastBit] != -1)
{
return dp[currentIndex,
adjacentSetBits, lastBit];
}
int noOfWays = 0;
/* Check if the last bit was set,
if it was set then call for
next index by incrementing the
adjacent bit count else just call
the next index with same value of
adjacent bit count and either set the
bit at current index or let it remain
unset */
if (lastBit == 1)
{
// set the bit at currentIndex
noOfWays += waysToKAdjacentSetBits(dp, n,
k, currentIndex + 1,
adjacentSetBits + 1, 1);
// unset the bit at currentIndex
noOfWays += waysToKAdjacentSetBits(dp, n,
k, currentIndex + 1,
adjacentSetBits, 0);
}
else if (lastBit==0)
{
noOfWays += waysToKAdjacentSetBits(dp,
n, k, currentIndex + 1,
adjacentSetBits, 1);
noOfWays += waysToKAdjacentSetBits(dp,
n, k, currentIndex + 1,
adjacentSetBits, 0);
}
dp[currentIndex,adjacentSetBits,lastBit] = noOfWays;
return noOfWays;
}
// Driver Code
public static void Main(String []args)
{
int n = 5, k = 2;
/* dp[i,j,k] represents bit strings
of length i with f(bit string) = j
and last bit as k */
int [,,]dp = new int[MAX, MAX, 2];
// initialize the dp
for(int i = 0; i < MAX; i++)
for(int j = 0; j < MAX; j++)
for(int k1 = 0; k1 < 2; k1++)
dp[i, j, k1]=-1;
/* total ways = (ways by placing 1st bit as 1 +
ways by placing 1st bit as 0) */
int totalWays = waysToKAdjacentSetBits(dp, n, k, 1, 0, 1)
+ waysToKAdjacentSetBits(dp, n, k, 1, 0, 0);
Console.Write( "Number of ways = " + totalWays + "\n");
}
}
// This code is contributed by PrinciRaj1992
JavaScript
<script>
// JavaScript program to find the number of Bit Strings
// of length N with K adjacent set bits
var MAX = 1000;
// Function to find the number of Bit Strings
// of length N with K adjacent set bits
function waysToKAdjacentSetBits(dp, n, k,
currentIndex, adjacentSetBits, lastBit)
{
/* Base Case when we form bit
string of length n */
if (currentIndex == n) {
// if f(bit string) = k, count this way
if (adjacentSetBits == k)
return 1;
return 0;
}
if (dp[currentIndex][adjacentSetBits][lastBit] != -1) {
return dp[currentIndex][adjacentSetBits][lastBit];
}
var noOfWays = 0;
/* Check if the last bit was set,
if it was set then call for
next index by incrementing the
adjacent bit count else just call
the next index with same value of
adjacent bit count and either set the
bit at current index or let it remain
unset */
if (lastBit == 1) {
// set the bit at currentIndex
noOfWays += waysToKAdjacentSetBits(dp, n, k,
currentIndex + 1, adjacentSetBits + 1, 1);
// unset the bit at currentIndex
noOfWays += waysToKAdjacentSetBits(dp, n, k,
currentIndex + 1, adjacentSetBits, 0);
}
else if (!lastBit) {
noOfWays += waysToKAdjacentSetBits(dp, n, k,
currentIndex + 1, adjacentSetBits, 1);
noOfWays += waysToKAdjacentSetBits(dp, n, k,
currentIndex + 1, adjacentSetBits, 0);
}
dp[currentIndex][adjacentSetBits][lastBit] = noOfWays;
return noOfWays;
}
// Driver Code
var n = 5, k = 2;
/* dp[i][j][k] represents bit strings of length i
with f(bit string) = j and last bit as k */
var dp = Array.from(Array(MAX), ()=>Array(MAX));
for(var i =0; i<MAX; i++)
for(var j =0; j<MAX; j++)
dp[i][j] = new Array(2).fill(-1);
/* total ways = (ways by placing 1st bit as 1 +
ways by placing 1st bit as 0) */
var totalWays = waysToKAdjacentSetBits(dp, n, k, 1, 0, 1)
+ waysToKAdjacentSetBits(dp, n, k, 1, 0, 0);
document.write( "Number of ways = " + totalWays + "<br>");
</script>
Efficient approach : Using DP Tabulation method ( Iterative approach )
The approach to solve this problem is same but DP tabulation(bottom-up) method is better then Dp + memoization(top-down) because memoization method needs extra stack space of recursion calls.
Steps to solve this problem :
- Create a DP to store the solution of the subproblems and initialize it with 0.
- Initializing the base case, when length of bit string is 1
dp[1][0][1] = 1;
dp[1][0][0] = 1; - Now Iterate over subproblems to get the value of current problem form previous computation of subproblems stored in DP.
- Now create a variable totalWays where totalWays = dp[n][k][0] + dp[n][k][1].
- Return the final solution stored in totalWays.
Implementation :
C++
// C++ program to find the number of Bit Strings
// of length N with K adjacent set bits
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000
// Function to find the number of Bit Strings
// of length N with K adjacent set bits
int waysToKAdjacentSetBits(int n, int k) {
// dp[i][j][k] represents bit strings of length i
// with f(bit string) = j and last bit as k
int dp[MAX][MAX][2];
memset(dp, 0, sizeof(dp));
// Initializing the base case
// when length of bit string is 1
dp[1][0][1] = 1;
dp[1][0][0] = 1;
// Filling up the DP Table
for(int i = 2; i <= n; i++) {
for(int j = 0; j <= k; j++) {
dp[i][j][0] = dp[i-1][j][0] + dp[i-1][j][1];
if(j > 0) {
dp[i][j][1] = dp[i-1][j-1][0];
}
dp[i][j][1] += dp[i-1][j][1];
}
}
// Total number of ways
int totalWays = dp[n][k][0] + dp[n][k][1];
return totalWays;
}
// Driver Code
int main() {
int n = 5, k = 2;
int totalWays = waysToKAdjacentSetBits(n, k);
cout << "Number of ways = " << totalWays << "\n";
return 0;
}
Java
// Java program to find the number of Bit Strings
// of length N with K adjacent set bits
import java.util.Arrays;
public class Main {
static final int MAX = 1000;
// Function to find the number of Bit Strings
// of length N with K adjacent set bits
static int waysToKAdjacentSetBits(int n, int k) {
// dp[i][j][k] represents bit strings of length i
// with f(bit string) = j and last bit as k
int[][][] dp = new int[MAX][MAX][2];
for (int[][] array2D : dp) {
for (int[] array1D : array2D) {
Arrays.fill(array1D, 0);
}
}
// Initializing the base case
// when length of bit string is 1
dp[1][0][1] = 1;
dp[1][0][0] = 1;
// Filling up the DP Table
for (int i = 2; i <= n; i++) {
for (int j = 0; j <= k; j++) {
dp[i][j][0] = dp[i - 1][j][0] + dp[i - 1][j][1];
if (j > 0) {
dp[i][j][1] = dp[i - 1][j - 1][0];
}
dp[i][j][1] += dp[i - 1][j][1];
}
}
// Total number of ways
int totalWays = dp[n][k][0] + dp[n][k][1];
return totalWays;
}
// Driver Code
public static void main(String[] args) {
int n = 5, k = 2;
int totalWays = waysToKAdjacentSetBits(n, k);
System.out.println("Number of ways = " + totalWays);
}
}
Python
# Function to find the number of Bit Strings
# of length N with K adjacent set bits
def waysToKAdjacentSetBits(n, k):
# dp[i][j][k] represents bit strings of length i
# with f(bit string) = j and last bit as k
dp = [[[0 for i in range(2)] for j in range(k + 1)] for l in range(n + 1)]
# Initializing the base case
# when length of bit string is 1
dp[1][0][1] = 1
dp[1][0][0] = 1
# Filling up the DP Table
for i in range(2, n + 1):
for j in range(k + 1):
dp[i][j][0] = dp[i - 1][j][0] + dp[i - 1][j][1]
if j > 0:
dp[i][j][1] = dp[i - 1][j - 1][0]
dp[i][j][1] += dp[i - 1][j][1]
# Total number of ways
totalWays = dp[n][k][0] + dp[n][k][1]
return totalWays
# Driver Code
if __name__ == '__main__':
n = 5
k = 2
totalWays = waysToKAdjacentSetBits(n, k)
print(f"Number of ways = {totalWays}")
C#
using System;
class MainClass {
static int WaysToKAdjacentSetBits(int n, int k) {
// dp[i][j][k] represents bit strings of length i
// with f(bit string) = j and last bit as k
int[,,] dp = new int[1000, 1000, 2];
// Initializing the base case
// when length of bit string is 1
dp[1, 0, 1] = 1;
dp[1, 0, 0] = 1;
// Filling up the DP Table
for(int i = 2; i <= n; i++) {
for(int j = 0; j <= k; j++) {
dp[i, j, 0] = dp[i-1, j, 0] + dp[i-1, j, 1];
if(j > 0) {
dp[i, j, 1] = dp[i-1, j-1, 0];
}
dp[i, j, 1] += dp[i-1, j, 1];
}
}
// Total number of ways
int totalWays = dp[n, k, 0] + dp[n, k, 1];
return totalWays;
}
public static void Main() {
int n = 5, k = 2;
int totalWays = WaysToKAdjacentSetBits(n, k);
Console.WriteLine("Number of ways = " + totalWays);
}
}
JavaScript
function waysToKAdjacentSetBits(n, k) {
// dp[i][j][k] represents bit strings of length i
// with f(bit string) = j and last bit as k
const dp = new Array(n + 1).fill(0).map(() => new Array(k + 1).fill(0).map(() => new Array(2).fill(0)));
// Initializing the base case
// when length of bit string is 1
dp[1][0][1] = 1;
dp[1][0][0] = 1;
// Filling up the DP Table
for (let i = 2; i <= n; i++) {
for (let j = 0; j <= k; j++) {
dp[i][j][0] = dp[i - 1][j][0] + dp[i - 1][j][1];
if (j > 0) {
dp[i][j][1] = dp[i - 1][j - 1][0];
}
dp[i][j][1] += dp[i - 1][j][1];
}
}
// Total number of ways
const totalWays = dp[n][k][0] + dp[n][k][1];
return totalWays;
}
// Driver Code
const n = 5, k = 2;
const totalWays = waysToKAdjacentSetBits(n, k);
console.log("Number of ways =", totalWays);
Time Complexity: O(n*k)
Auxiliary Space: O(n*k)
Similar Reads
Count of Binary Strings of length at most N with set bit count as multiple of K
Given two integers N and K, the task is to find the count of binary strings of at most N length that can be formed such that the count of consecutive 1's is always a multiple of K. Example: Input: N = 3, K = 2Output: 6Explanation: Binary strings of atmost N length containing consecutive 1's as a mul
7 min read
Number of binary strings such that there is no substring of length ⥠3
Given an integer N, the task is to count the number of binary strings possible such that there is no substring of length ? 3 of all 1's. This count can become very large so print the answer modulo 109 + 7.Examples: Input: N = 4 Output: 13 All possible valid strings are 0000, 0001, 0010, 0100, 1000,
10 min read
Count number of binary strings of length N having only 0's and 1's
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 Expl
6 min read
Number of substrings with length divisible by the number of 1's in it
Given a binary string S consisting of only 0's and 1's. Count the number of substrings of this string such that the length of the substring is divisible by the number of 1's in the substring. Examples: Input: S = "01010" Output: 10 Input: S = "1111100000" Output: 25 Naive Approach: Iterate through a
15+ min read
Count of pairs in an Array with same number of set bits
Given an array arr containing N integers, the task is to count the possible number of pairs of elements with the same number of set bits. Examples: Input: N = 8, arr[] = {1, 2, 3, 4, 5, 6, 7, 8} Output: 9 Explanation: Elements with 1 set bit: 1, 2, 4, 8 Elements with 2 set bits: 3, 5, 6 Elements wit
7 min read
Count of binary strings of length N with even set bit count and at most K consecutive 1s
Given two integers N and K, the task is to find the number of binary strings of length N having an even number of 1's out of which less than K are consecutive.Examples: Input: N = 4, K = 2 Output: 4 Explanation: The possible binary strings are 0000, 0101, 1001, 1010. They all have even number of 1's
12 min read
Count number of binary strings without consecutive 1âs : Set 2
Given a positive integer N, the task is to count all possible distinct binary strings of length N such that there are no consecutive 1âs. Examples: Input: N = 5 Output: 5 Explanation: The non-negative integers <= 5 with their corresponding binary representations are: 0 : 0 1 : 1 2 : 10 3 : 11 4 :
15+ min read
Maximum number of set bits count in a K-size substring of a Binary String
Given a binary string S of size N and an integer K. The task is to find the maximum number of set bit appears in a substring of size K. Examples: Input: S = "100111010", K = 3 Output: 3 Explanation: The substring "111" contains 3 set bits. Input:S = "0000000", K = 4 Output: 0 Explanation: S doesn't
10 min read
Count of binary string of length N with X 0s and Y 1s
Given positive integers N, X and Y. The task is to find the count of unique binary strings of length N having X 0s and Y 1s. Examples: Input: N=5, X=3, Y=2Output: 10Explanation: There are 10 binary strings of length 5 with 3 0s and 2 1s, such as: 00011, 00101, 01001, 10001, 00110, 01010, 10010, 0110
4 min read
Number of substrings with odd decimal value in a binary string
Given a binary string containing only 0's and 1's. Write a program to find number of sub-strings of this string whose decimal representation is odd. Examples : Input : 101 Output : 3 Explanation : Substrings with odd decimal representation are: {1, 1, 101} Input : 1101 Output : 6 Explanation : Subst
6 min read