# Count Pairs Of Consecutive Zeros

• Difficulty Level : Medium
• Last Updated : 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++

 `// C++ program to find number of consecutive``// 0s in a sequence``#include``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.`

## PHP

 ``

## Javascript

 ``

Output :

`5`