Count Pairs Of Consecutive Zeros
Last Updated :
04 Oct, 2023
Consider a sequence that starts with a 1 on a machine. At each successive step, the machine simultaneously transforms each digit 0 into the sequence 10 and each digit 1 into sequence 01.
After the first time step, the sequence 01 is obtained; after the second, the sequence 1001, after the third, the sequence 01101001, and so on.
How many pairs of consecutive zeros will appear in the sequence after n steps?
Examples :
Input: Number of steps = 3
Output: 1
// After 3rd step sequence will be 01101001
Input: Number of steps = 4
Output: 3
// After 4rd step sequence will be 1001011001101001
Input: Number of steps = 5
Output: 5
// After 3rd step sequence will be 01101001100101101001011001101001
Brute Force Approach:
To find the number of pairs of consecutive zeros in the sequence, we can first generate the sequence up to the given number of steps, and then count the number of pairs of consecutive zeros in the sequence.
One way to generate the sequence is to use a recursive function that simulates the machine’s transformation at each step. Starting with a 1, we can apply the transformation to each digit of the previous sequence to obtain the next sequence.
Implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string generateSequence( int n, string seq) {
if (n == 0) {
return seq;
} else {
string nextSeq = "" ;
for ( char c : seq) {
if (c == '0' ) {
nextSeq += "10" ;
} else {
nextSeq += "01" ;
}
}
return generateSequence(n - 1, nextSeq);
}
}
int countPairs(string seq) {
int count = 0;
for ( int i = 0; i < seq.size() - 1; i++) {
if (seq[i] == '0' && seq[i + 1] == '0' ) {
count++;
}
}
return count;
}
int consecutiveZeroPairs( int n) {
string seq = generateSequence(n, "1" );
return countPairs(seq);
}
int main() {
int n = 5;
cout << consecutiveZeroPairs(n) << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
static String generateSequence( int n, String seq) {
if (n == 0 ) {
return seq;
} else {
String nextSeq = "" ;
for ( char c : seq.toCharArray()) {
if (c == '0' ) {
nextSeq += "10" ;
} else {
nextSeq += "01" ;
}
}
return generateSequence(n - 1 , nextSeq);
}
}
static int countPairs(String seq) {
int count = 0 ;
for ( int i = 0 ; i < seq.length() - 1 ; i++) {
if (seq.charAt(i) == '0' && seq.charAt(i + 1 ) == '0' ) {
count++;
}
}
return count;
}
static int consecutiveZeroPairs( int n) {
String seq = generateSequence(n, "1" );
return countPairs(seq);
}
public static void main(String[] args) {
int n = 5 ;
System.out.println(consecutiveZeroPairs(n));
}
}
|
Python3
def generate_sequence(n, seq):
if n = = 0 :
return seq
else :
next_seq = ""
for c in seq:
if c = = '0' :
next_seq + = "10"
else :
next_seq + = "01"
return generate_sequence(n - 1 , next_seq)
def count_pairs(seq):
count = 0
for i in range ( len (seq) - 1 ):
if seq[i] = = '0' and seq[i + 1 ] = = '0' :
count + = 1
return count
def consecutive_zero_pairs(n):
seq = generate_sequence(n, "1" )
return count_pairs(seq)
if __name__ = = "__main__" :
n = 5
print (consecutive_zero_pairs(n))
|
C#
using System;
public class Program
{
static string GenerateSequence( int n, string seq)
{
if (n == 0)
{
return seq;
}
else
{
string nextSeq = "" ;
foreach ( char c in seq)
{
if (c == '0' )
{
nextSeq += "10" ;
}
else
{
nextSeq += "01" ;
}
}
return GenerateSequence(n - 1, nextSeq);
}
}
static int CountPairs( string seq)
{
int count = 0;
for ( int i = 0; i < seq.Length - 1; i++)
{
if (seq[i] == '0' && seq[i + 1] == '0' )
{
count++;
}
}
return count;
}
static int ConsecutiveZeroPairs( int n)
{
string seq = GenerateSequence(n, "1" );
return CountPairs(seq);
}
static void Main()
{
int n = 5;
Console.WriteLine(ConsecutiveZeroPairs(n));
}
}
|
Javascript
function generateSequence(n, seq) {
if (n === 0) {
return seq;
} else {
let nextSeq = "" ;
for (let c of seq) {
if (c === '0' ) {
nextSeq += "10" ;
} else {
nextSeq += "01" ;
}
}
return generateSequence(n - 1, nextSeq);
}
}
function countPairs(seq) {
let count = 0;
for (let i = 0; i < seq.length - 1; i++) {
if (seq[i] === '0' && seq[i + 1] === '0' ) {
count++;
}
}
return count;
}
function consecutiveZeroPairs(n) {
let seq = generateSequence(n, "1" );
return countPairs(seq);
}
let n = 5;
console.log(consecutiveZeroPairs(n));
|
Time Complexity: O(N^2)
Auxiliary Space: O(N)
Approach:
This is a simple reasoning problem. If we see the sequence very carefully , then we will be able to find a pattern for given sequence. If n=1 sequence will be {01} so number of pairs of consecutive zeros are 0, If n = 2 sequence will be {1001} so number of pairs of consecutive zeros are 1, If n=3 sequence will be {01101001} so number of pairs of consecutive zeros are 1,
If n=4 sequence will be {1001011001101001} so number of pairs of consecutive zeros are 3.
So length of the sequence will always be a power of 2. We can see after length 12 sequence is repeating and in lengths of 12. And in a segment of length 12, there are total 2 pairs of consecutive zeros. Hence we can generalize the given pattern q = (2^n/12) and total pairs of consecutive zeros will be 2*q+1.
C++
#include<bits/stdc++.h>
using namespace std;
int consecutiveZeroPairs( int n)
{
if (n==1)
return 0;
if (n==2 || n==3)
return 1;
int q = ( pow (2, n) / 12);
return 2 * q + 1;
}
int main()
{
int n = 5;
cout << consecutiveZeroPairs(n) << endl;
return 0;
}
|
Java
import java.io.*;
import java.math.*;
class GFG {
static int consecutiveZeroPairs( int n)
{
if (n == 1 )
return 0 ;
if (n == 2 || n == 3 )
return 1 ;
int q = (( int )(Math.pow( 2 , n)) / 12 );
return ( 2 * q + 1 );
}
public static void main(String args[])
{
int n = 5 ;
System.out.println(consecutiveZeroPairs(n));
}
}
|
Python3
import math
def consecutiveZeroPairs(n) :
if (n = = 1 ) :
return 0
if (n = = 2 or n = = 3 ) :
return 1
q = ( int ) ( pow ( 2 ,n) / 12 )
return 2 * q + 1
n = 5
print (consecutiveZeroPairs(n))
|
C#
using System;
class GFG {
static int consecutiveZeroPairs( int n)
{
if (n == 1)
return 0;
if (n == 2 || n == 3)
return 1;
int q = (( int )(Math.Pow(2, n)) / 12);
return (2 * q + 1);
}
public static void Main()
{
int n = 5;
Console.Write(consecutiveZeroPairs(n));
}
}
|
Javascript
<script>
function consecutiveZeroPairs(n)
{
if (n == 1)
return 0;
if (n == 2 || n == 3)
return 1;
var q =(parseInt((Math.pow(2, n)) / 12));
return (2 * q + 1);
}
var n = 5;
document.write(consecutiveZeroPairs(n));
</script>
|
PHP
<?php
function consecutiveZeroPairs( $n )
{
if ( $n == 1)
return 0;
if ( $n == 2 || $n == 3)
return 1;
$q = floor (pow(2, $n ) / 12);
return 2 * $q + 1;
}
$n = 5;
echo consecutiveZeroPairs( $n ) ;
?>
|
Output :
5
Time Complexity: O(log(n))
Auxiliary Space: O(1)
this article is reviewed by team GeeksForGeeks.
Share your thoughts in the comments
Please Login to comment...