
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
- Related Articles
- Python program to count pairs for consecutive elements
- Count of pairs in an array that have consecutive numbers using JavaScript
- Count number of pairs (A
- Count trailing zeros in factorial of a number in C++
- Count number of trailing zeros in product of array in C++
- Count passing car pairs in C++
- Python – Consecutive identical elements count
- Maximum consecutive one’s (or zeros) in a binary circular array in C++
- Count pairs with given sum in C++
- Count pairs with Odd XOR in C++
- Program to find array by swapping consecutive index pairs in Python
- Count divisible pairs in an array in C++
- Count the pairs of vowels in the given string in C++
- Count the number of key/value pairs in HybridDictionary in C#
- Count of arrays having consecutive element with different values in C++
