- 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 ways to reach a score using 1 and 2 with no consecutive 2s in C++

Given a score of runs. The goal is to reach that score in a way that the batsman can take either 1 or 2 runs only in a single ball. The restriction is that no 2 runs can be taken consecutively. For example, to reach the given score 6, one can take runs like: 1+2+1+2 but not 2+2+1+1 or any other way with two consecutive 2’s.

**For Example**

## Input

score=4

## Output

Count of ways to reach a score using 1 and 2 with no consecutive 2s are: 4

## Explanation

The ways in which we can reach the score 4 in following ways: 1+1+1+1, 1+1+2, 1+2+1, 2+1+1

## Input

score=5

## Output

Count of ways to reach a score using 1 and 2 with no consecutive 2s are: 6

## Explanation

The ways in which we can reach the score 6 in following ways: 1+1+1+1+1, 2+1+1+1, 1+2+1+1 , 1+1+2+1, 1+1+1+2, 2+1+2

**Approach used in the below program is as follows** −

In this approach we will use a flag to mark that previous score was two or not, in case it was 2 we will cover the score with the next run as 1 else 2.

Take an integer variable score.

Take flag variable check = false initially.

Function ways_reach_score(int score, bool check) tells the count of ways to reach a score using 1 and 2 with no consecutive 2s.

Take the initial count as 0.

If the score is 0 return 0.

If the check is false means previous run was 1, so in case current score is greater then ways will be count = ways_reach_score(score − 1, false) + ways_reach_score(score − 2, true).

Otherwise the previous run was 2, so the consequent run will be 1 only so ways will be ways_reach_score(score − 1, false).

At the end we will get total ways in count.

Return count as result.

## Example

#include <bits/stdc++.h> using namespace std; int ways_reach_score(int score, bool check){ int count = 0; if (score == 0){ return 1; } if (check == false && score > 1){ count += ways_reach_score(score − 1, false) + ways_reach_score(score − 2, true); } else { count += ways_reach_score(score − 1, false); } return count; } int main(){ int score = 4; bool check = false; cout<<"Count of ways to reach a score using 1 and 2 with no consecutive 2s are: "<<ways_reach_score(score, check); return 0; }

## Output

If we run the above code it will generate the following output −

Count of ways to reach a score using 1 and 2 with no consecutive 2s are: 4

- Related Articles
- Count number of ways to reach a given score in a Matrix in C++
- Count number of ways to reach a given score in a game
- Count ways to reach the nth stair using step 1, 2 or 3 in C++
- Count number of ways to reach destination in a Maze in C++
- Count number of ways to jump to reach end in C++
- Count ways to reach the n’th stair
- Count Subarrays with Consecutive elements differing by 1 in C++
- Count Substrings with equal number of 0s, 1s and 2s in C++
- Count ways to express a number as sum of consecutive numbers in C++
- 1 to n bit numbers with no consecutive 1s in binary representation?
- Count ways to spell a number with repeated digits in C++
- Program to Count number of binary strings without consecutive 1’s in C/C++?
- C/C++ Program to Count number of binary strings without consecutive 1’s?
- Count ways of choosing a pair with maximum difference in C++
- Count number of binary strings without consecutive 1's in C