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<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; ` `} ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## Python

`# 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. ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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. ` `?> ` |

*chevron_right*

*filter_none*

**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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.