 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count Pairs of Consecutive Zeros in C++
We have a sequence generator that starts with 1. At each step 0 becomes 10 and 1 becomes 01. So following changes will occur at consecutive steps −
Step 1 − 01
Step 2 − 1001
Step 3 − 01101001 ……
The goal is to find the number of pairs of consecutive 0’s for a given number of steps.
If input step is 1 pair of 0’s - 0, input step is 2 pair of 0’s - 1, input step is 3 pair of 0’s 1
Step 4 − 1001011001101001
Step 5 − 01101001100101101001011001101001
We can observe that sequence is increasing in powers of 2 and repeats itself after length 12 and for every 12 characters. So, these 12 length sequences have 2 pairs of consecutive 0’s.
For steps S number of 12 length patterns will be 2S /12
Number of consecutive 2 patterns = 1 (initial) + 2 X S ( for rest of 12 length repetitions )
Let us understand with examples.
Input − steps = 5
Output − Count of Pairs Of Consecutive Zeros are − 5
Explanation − As shown above sequence at 5th step is −
Step 5: 01101001100101101001011001101001 Number of pairs of 0’s is 5. Also with formula : tmp=25 /12= 32/12 = 2, pairs=1+ 2 x 2 = 5
Input − steps = 10
Output − Count of Pairs Of Consecutive Zeros are − 171
Explanation − With formula − tmp=210/12= 1024/12 = 85, pairs=1+ 2 x 85 = 171
The approach used in the below program is as follows
We will take the number of steps as input and check if steps=1 then the number of pairs of consecutive 0’s is 0. If steps = 2 or 3 then the number of such pairs is 1.
Otherwise, calculate using formula tmp=2input/12 and pairs=1+2*tmp.
- Take a variable decimal as input for steps. 
- Function Zero_pairs(int decimal) takes decimal and returns Count of Pairs Of Consecutive Zeros for that step. 
- Take the initial count as 0. 
- If decimal<=1, return 0. 
- If decimal==2 or decimal==3. Then return 1. 
- Else calculate temp=(pow(2,decimal))/12 and calculate count as 2*temp + 1. 
- Return count as result. 
Example
#include<bits/stdc++.h> using namespace std; int Zero_pairs(int decimal){    int count = 0;    if(decimal <=1){       count = 0;    }    else if(decimal == 2 || decimal == 3){       count = 1;    }    else{       int temp = (pow(2, decimal) / 12);       count = 2 * temp + 1;    }    return count; } int main(){    int decimal = 7;    cout<<"Count of Pairs Of Consecutive Zeros are: "<<Zero_pairs(decimal);    return 0; } Output
If we run the above code it will generate the following output −
Count of Pairs Of Consecutive Zeros are: 21
