Count Pairs Of Consecutive Zeros - GeeksforGeeks

22 Jan, 2022
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 the 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
 
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++
Java
Python3
C#
PHP
Javascript
// 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 stepsint 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 codeint main(){    int n = 5;    cout << consecutiveZeroPairs(n) << endl;    return 0;}
//Java program to find number of// consecutive 0s in a sequenceimport 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.
# Python program to find number of# consecutive 0s in a sequenceimport math # Function to find number of consecutive# Zero Pairs. Here n is number of stepsdef 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 coden = 5print (consecutiveZeroPairs(n)) #This code is contributed by Nikita Tiwari.
// C# program to find number of// consecutive 0s in a sequenceusing 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.
<?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 stepsfunction 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.?>
<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>
Output :  
5
This article is contributed by Shashank Mishra ( Gullu ). this article is reviewed by team GeeksForGeeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
nitin mittal
umadevi9616
amartyaghoshgfg
Combinatorial
Combinatorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Combinations with repetitions
Ways to sum to N using Natural Numbers up to K with repetitions allowed
Largest number by rearranging digits of a given positive or negative number
Given number of matches played, find number of teams in tournament
Generate all possible combinations of at most X characters from a given array
Number of Simple Graph with N Vertices and M Edges
Largest substring with same Characters
Minimum cost to convert 1 to N by multiplying X or right rotation of digits
Print all Strong numbers less than or equal to N
Combinations from n arrays picking one element from each array