Open In App

Count Pairs Of Consecutive Zeros

Last Updated : 04 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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;
 
// Recursive function to generate the sequence
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);
    }
}
 
// Function to count pairs of consecutive zeros
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;
}
 
// Function to find number of consecutive zero pairs
int consecutiveZeroPairs(int n) {
    string seq = generateSequence(n, "1");
    return countPairs(seq);
}
 
// Driver code
int main() {
    int n = 5;
    cout << consecutiveZeroPairs(n) << endl;
    return 0;
}


Java




import java.util.*;
 
public class Main {
    // Recursive function to generate the sequence
    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);
        }
    }
 
    // Function to count pairs of consecutive zeros
    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;
    }
 
    // Function to find number of consecutive zero pairs
    static int consecutiveZeroPairs(int n) {
        String seq = generateSequence(n, "1");
        return countPairs(seq);
    }
 
    // Driver code
    public static void main(String[] args) {
        int n = 5;
        System.out.println(consecutiveZeroPairs(n));
    }
}
// This code is contributed by Prajwal Kandekar


Python3




# Recursive function to generate the sequence
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)
 
# Function to count pairs of consecutive zeros
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
 
# Function to find the number of consecutive zero pairs
def consecutive_zero_pairs(n):
    seq = generate_sequence(n, "1")
    return count_pairs(seq)
 
# Driver code
if __name__ == "__main__":
    n = 5
    print(consecutive_zero_pairs(n))


C#




using System;
 
public class Program
{
    // Recursive function to generate the sequence
    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);
        }
    }
 
    // Function to count pairs of consecutive zeros
    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;
    }
 
    // Function to find number of consecutive zero pairs
    static int ConsecutiveZeroPairs(int n)
    {
        string seq = GenerateSequence(n, "1");
        return CountPairs(seq);
    }
 
    // Driver code
    static void Main()
    {
        int n = 5;
        Console.WriteLine(ConsecutiveZeroPairs(n));
    }
}


Javascript




//Javascript code for the above approach
 
// Recursive function to generate the sequence
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 to count pairs of consecutive zeros
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 to find number of consecutive zero pairs
function consecutiveZeroPairs(n) {
    let seq = generateSequence(n, "1");
    return countPairs(seq);
}
 
// Driver code
let n = 5;
console.log(consecutiveZeroPairs(n));


Output

5



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++




// C++ program to find number of consecutive
// 0s in a sequence
#include<bits/stdc++.h>
using namespace std;
 
// Function to find number of consecutive Zero Pairs
// Here n is number of steps
int consecutiveZeroPairs(int n)
{
    // Base cases
    if (n==1)
        return 0;
    if (n==2 || n==3)
        return 1;
 
    // Calculating how many times divisible by 12, i.e.,
    // count total number repeating segments of length 12
    int q = (pow(2, n) / 12);
 
    // number of consecutive Zero Pairs
    return 2 * q + 1;
}
 
// Driver code
int main()
{
    int n = 5;
    cout << consecutiveZeroPairs(n) << endl;
    return 0;
}


Java




//Java program to find number of
// consecutive 0s in a sequence
import java.io.*;
import java.math.*;
 
class GFG {
     
    // Function to find number of consecutive
    // Zero Pairs. Here n is number of steps
    static int consecutiveZeroPairs(int n)
    {
        // Base cases
        if (n == 1)
            return 0;
        if (n == 2 || n == 3)
            return 1;
 
        // Calculating how many times divisible
        // by 12, i.e.,count total number
        // repeating segments of length 12
        int q = ((int)(Math.pow(2, n)) / 12);
 
        // number of consecutive Zero Pairs
        return (2 * q + 1);
    }
 
    // Driver code
    public static void main(String args[])
    {
        int n = 5;
        System.out.println(consecutiveZeroPairs(n));
    }
}
     
// This code is contributed by Nikita Tiwari.


Python3




# Python program to find number of
# consecutive 0s in a sequence
import math
 
# Function to find number of consecutive
# Zero Pairs. Here n is number of steps
def consecutiveZeroPairs(n) :
 
    # Base cases
    if (n == 1) :
        return 0
    if (n == 2 or n == 3) :
        return 1
 
    # Calculating how many times divisible
    # by 12, i.e.,count total number
    # repeating segments of length 12
    q =(int) (pow(2,n) / 12)
 
    # number of consecutive Zero Pairs
    return 2 * q + 1
 
# Driver code
n = 5
print (consecutiveZeroPairs(n))
 
#This code is contributed by Nikita Tiwari.


C#




// C# program to find number of
// consecutive 0s in a sequence
using System;
 
class GFG {
     
    // Function to find number of
    // consecutive Zero Pairs.
    // Here n is number of steps
    static int consecutiveZeroPairs(int n)
    {
        // Base cases
        if (n == 1)
            return 0;
        if (n == 2 || n == 3)
            return 1;
 
        // Calculating how many times divisible
        // by 12, i.e.,count total number
        // repeating segments of length 12
        int q = ((int)(Math.Pow(2, n)) / 12);
 
        // number of consecutive Zero Pairs
        return (2 * q + 1);
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 5;
        Console.Write(consecutiveZeroPairs(n));
    }
}
     
// This code is contributed by Nitin mittal.


Javascript




<script>
//javascript program to find number of
// consecutive 0s in a sequence
 
    // Function to find number of consecutive
    // Zero Pairs. Here n is number of steps
    function consecutiveZeroPairs(n)
    {
        // Base cases
        if (n == 1)
            return 0;
        if (n == 2 || n == 3)
            return 1;
 
        // Calculating how many times divisible
        // by 12, i.e.,count total number
        // repeating segments of length 12
        var q =(parseInt((Math.pow(2, n)) / 12));
 
        // number of consecutive Zero Pairs
        return (2 * q + 1);
    }
 
    // Driver code
 
        var n = 5;
        document.write(consecutiveZeroPairs(n));
 
// This code is contributed by umadevi9616
</script>


PHP




<?php
// PHP program to find number
// of consecutive 0s in a sequence
 
// Function to find number
// of consecutive Zero Pairs
// Here n is number of steps
function consecutiveZeroPairs($n)
{
    // Base cases
    if ($n == 1)
        return 0;
    if ($n == 2 || $n == 3)
        return 1;
 
    // Calculating how many times
    // divisible by 12, i.e., count
    // total number repeating segments
    // of length 12
    $q = floor(pow(2, $n) / 12);
 
    // number of consecutive Zero Pairs
    return 2 * $q + 1;
}
 
// Driver code
$n = 5;
echo consecutiveZeroPairs($n) ;
 
// This code is contributed
// by nitin mittal.
?>


Output : 
 

5

Time Complexity: O(log(n))
Auxiliary Space: O(1)

this article is reviewed by team GeeksForGeeks. 

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads