Bit Stuffing is a process of inserting an extra bit as 0, once the frame sequence encountered 5 consecutive 1’s. Given an array, arr[] of size N consisting of 0’s and 1’s, the task is to return an array after the bit stuffing.
Examples:
Input: N = 6, arr[] = {1, 1, 1, 1, 1, 1}
Output: 1111101
Explanation: During the traversal of the array, 5 consecutive 1’s are encountered after the 4th index of the given array. Hence, a zero bit has been inserted into the stuffed array after the 4th index
Input: N = 6, arr[] = {1, 0, 1, 0, 1, 0}
Output: 101010
Approach: The idea is to check if the given array consists of 5 consecutive 1’s. Follow the steps below to solve the problem:
- Initialize the array brr[] which stores the stuffed array. Also, create a variable count which maintains the count of the consecutive 1’s.
- Traverse in a while loop using a variable i in the range [0, N) and perform the following tasks:
- If arr[i] is 1 then check for the next 4 bits if they are set bits as well. If they are, then insert a 0 bit after inserting all the 5 set bits into the array brr[].
- Otherwise, insert the value of arr[i] into the array brr[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
void bitStuffing( int N, int arr[])
{
int brr[30];
int i, j, k;
i = 0;
j = 0;
while (i < N) {
if (arr[i] == 1) {
int count = 1;
brr[j] = arr[i];
for (k = i + 1;
arr[k] == 1 && k < N && count < 5; k++) {
j++;
brr[j] = arr[k];
count++;
if (count == 5) {
j++;
brr[j] = 0;
}
i = k;
}
}
else {
brr[j] = arr[i];
}
i++;
j++;
}
for (i = 0; i < j; i++)
cout << brr[i];
}
int main()
{
int N = 6;
int arr[] = { 1, 1, 1, 1, 1, 1 };
bitStuffing(N, arr);
return 0;
}
|
C
#include <stdio.h>
#include <string.h>
void bitStuffing( int N, int arr[])
{
int brr[30];
int i, j, k;
i = 0;
j = 0;
while (i < N) {
if (arr[i] == 1) {
int count = 1;
brr[j] = arr[i];
for (k = i + 1;
arr[k] == 1 && k < N && count < 5; k++) {
j++;
brr[j] = arr[k];
count++;
if (count == 5) {
j++;
brr[j] = 0;
}
i = k;
}
}
else {
brr[j] = arr[i];
}
i++;
j++;
}
for (i = 0; i < j; i++)
printf ( "%d" , brr[i]);
}
int main()
{
int N = 6;
int arr[] = { 1, 1, 1, 1, 1, 1 };
bitStuffing(N, arr);
return 0;
}
|
Java
class GFG {
static void bitStuffing( int N, int arr[])
{
int [] brr = new int [ 30 ];
int i, j, k;
i = 0 ;
j = 0 ;
while (i < N) {
if (arr[i] == 1 ) {
int count = 1 ;
brr[j] = arr[i];
for (k = i + 1 ; k < N && arr[k] == 1
&& count < 5 ;
k++) {
j++;
brr[j] = arr[k];
count++;
if (count == 5 ) {
j++;
brr[j] = 0 ;
}
i = k;
}
}
else {
brr[j] = arr[i];
}
i++;
j++;
}
for (i = 0 ; i < j; i++)
System.out.printf( "%d" , brr[i]);
}
public static void main(String[] args)
{
int N = 6 ;
int arr[] = { 1 , 1 , 1 , 1 , 1 , 1 };
bitStuffing(N, arr);
}
}
|
Python3
def bitStuffing(N, arr):
brr = [ 0 for _ in range ( 30 )]
k = 0
i = 0
j = 0
while (i < N):
if (arr[i] = = 1 ):
count = 1
brr[j] = arr[i]
k = i + 1
while True :
if not (k < N and arr[k] = = 1 and
count < 5 ):
break
j + = 1
brr[j] = arr[k]
count + = 1
if (count = = 5 ):
j + = 1
brr[j] = 0
i = k
k + = 1
else :
brr[j] = arr[i]
i + = 1
j + = 1
for i in range ( 0 , j):
print (brr[i], end = "")
if __name__ = = "__main__" :
N = 6
arr = [ 1 , 1 , 1 , 1 , 1 , 1 ]
bitStuffing(N, arr)
|
C#
using System;
class GFG {
static void bitStuffing( int N, int [] arr)
{
int [] brr = new int [30];
int i, j, k;
i = 0;
j = 0;
while (i < N) {
if (arr[i] == 1) {
int count = 1;
brr[j] = arr[i];
k = i + 1;
while (k < N && arr[k] == 1 && count < 5) {
j++;
brr[j] = arr[k];
count++;
if (count == 5) {
j++;
brr[j] = 0;
}
i = k;
k++;
}
}
else {
brr[j] = arr[i];
}
i++;
j++;
}
for (i = 0; i < j; i++)
Console.Write(brr[i]);
}
public static void Main()
{
int N = 6;
int [] arr = { 1, 1, 1, 1, 1, 1 };
bitStuffing(N, arr);
}
}
|
Javascript
<script>
function bitStuffing(N, arr)
{
let brr = new Array(30);
let i, j, k;
i = 0;
j = 0;
while (i < N) {
if (arr[i] == 1) {
let count = 1;
brr[j] = arr[i];
for (k = i + 1;
arr[k] == 1
&& k < N
&& count < 5;
k++) {
j++;
brr[j] = arr[k];
count++;
if (count == 5) {
j++;
brr[j] = 0;
}
i = k;
}
}
else {
brr[j] = arr[i];
}
i++;
j++;
}
for (i = 0; i < j; i++)
document.write(brr[i] + " " );
}
let N = 6;
let arr = [1, 1, 1, 1, 1, 1];
bitStuffing(N, arr);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Bit Destuffing or Bit Unstuffing is a process of undoing the changes in the array made during the bit stuffing process i.e, removing the extra 0 bit after encountering 5 consecutive 1’s.
Examples:
Input: N = 7, arr[] = {1, 1, 1, 1, 1, 0, 1}
Output: 111111
Explanation: During the traversal of the array, 5 consecutive 1’s are encountered after the 4th index of the given array. Hence, the next 0 bit must be removed to de-stuffed array.
Input: N = 6, arr[] = {1, 0, 1, 0, 1, 0}
Output: 101010
Approach: This problem can be solved similarly to the bit stuffing problem. The only required change in the above-discussed approach is whenever 5 consecutive 1’s are encountered, skip the next bit in the array arr[] in place of inserting a 0 bit in the array brr[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void bitDestuffing( int N, int arr[])
{
int brr[30];
int i, j, k;
i = 0;
j = 0;
int count = 1;
while (i < N) {
if (arr[i] == 1) {
brr[j] = arr[i];
for (k = i + 1; arr[k] == 1 && k < N && count < 5; k++) {
j++;
brr[j] = arr[k];
count++;
if (count == 5) {
k++;
}
i = k;
}
}
else {
brr[j] = arr[i];
}
i++;
j++;
}
for (i = 0; i < j; i++)
cout<< brr[i];
}
int main()
{
int N = 7;
int arr[] = { 1, 1, 1, 1, 1, 0, 1 };
bitDestuffing(N, arr);
return 0;
}
|
C
#include <stdio.h>
#include <string.h>
void bitDestuffing( int N, int arr[])
{
int brr[30];
int i, j, k;
i = 0;
j = 0;
int count = 1;
while (i < N) {
if (arr[i] == 1) {
brr[j] = arr[i];
for (k = i + 1;
arr[k] == 1
&& k < N
&& count < 5;
k++) {
j++;
brr[j] = arr[k];
count++;
if (count == 5) {
k++;
}
i = k;
}
}
else {
brr[j] = arr[i];
}
i++;
j++;
}
for (i = 0; i < j; i++)
printf ( "%d" , brr[i]);
}
int main()
{
int N = 7;
int arr[] = { 1, 1, 1, 1, 1, 0, 1 };
bitDestuffing(N, arr);
return 0;
}
|
Java
class GFG{
static void bitDestuffing( int N, int arr[])
{
int []brr = new int [ 30 ];
int i, j, k;
i = 0 ;
j = 0 ;
int count = 1 ;
while (i < N) {
if (arr[i] == 1 ) {
brr[j] = arr[i];
for (k = i + 1 ; k<N &&
arr[k] == 1
&& count < 5 ;
k++) {
j++;
brr[j] = arr[k];
count++;
if (count == 5 ) {
k++;
}
i = k;
}
}
else {
brr[j] = arr[i];
}
i++;
j++;
}
for (i = 0 ; i < j; i++)
System.out.printf( "%d" , brr[i]);
}
public static void main(String[] args)
{
int N = 7 ;
int arr[] = { 1 , 1 , 1 , 1 , 1 , 0 , 1 };
bitDestuffing(N, arr);
}
}
|
Python3
def bitDestuffing(N, arr):
brr = [ 0 for i in range ( 30 )];
k = 0 ;
i = 0 ;
j = 0 ;
count = 1 ;
while (i < N):
if (arr[i] = = 1 ):
brr[j] = arr[i];
for k in range (i + 1 , k < N and arr[k] = = 1 and count < 5 , 1 ):
j + = 1 ;
brr[j] = arr[k];
count + = 1 ;
if (count = = 5 ):
k + = 1 ;
i = k;
else :
brr[j] = arr[i];
i + = 1 ;
j + = 1 ;
for i in range ( 0 , j):
print (brr[i],end = "");
if __name__ = = '__main__' :
N = 7 ;
arr = [ 1 , 1 , 1 , 1 , 1 , 0 , 1 ];
bitDestuffing(N, arr);
|
C#
using System;
class GFG{
static void bitDestuffing( int N, int [] arr)
{
int []brr = new int [30];
int i, j, k;
i = 0;
j = 0;
int count = 1;
while (i < N) {
if (arr[i] == 1) {
brr[j] = arr[i];
for (k = i + 1; k<N &&
arr[k] == 1
&& count < 5;
k++) {
j++;
brr[j] = arr[k];
count++;
if (count == 5) {
k++;
}
i = k;
}
}
else {
brr[j] = arr[i];
}
i++;
j++;
}
for (i = 0; i < j; i++)
Console.Write(brr[i]);
}
public static void Main()
{
int N = 7;
int [] arr = { 1, 1, 1, 1, 1, 0, 1 };
bitDestuffing(N, arr);
}
}
|
Javascript
<script>
function bitDestuffing(N , arr)
{
var brr = Array.from({length: 30}, (_, i) => 0);
var i, j, k;
i = 0;
j = 0;
var count = 1;
while (i < N) {
if (arr[i] == 1) {
brr[j] = arr[i];
for (k = i + 1; k<N &&
arr[k] == 1
&& count < 5;
k++) {
j++;
brr[j] = arr[k];
count++;
if (count == 5) {
k++;
}
i = k;
}
}
else {
brr[j] = arr[i];
}
i++;
j++;
}
for (i = 0; i < j; i++)
document.write(brr[i]);
}
var N = 7;
var arr = [ 1, 1, 1, 1, 1, 0, 1 ];
bitDestuffing(N, arr);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
11 Jan, 2023
Like Article
Save Article