- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- 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

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

score=4

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

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

score=5

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

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.

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

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 Questions & Answers
- Count ways to reach the nth stair using step 1, 2 or 3 in C++
- Count number of ways to reach a given score in a game
- Count number of ways to reach a given score in a Matrix in C++
- Count number of ways to reach destination in a Maze in C++
- Count Subarrays with Consecutive elements differing by 1 in C++
- Count number of ways to jump to reach end in C++
- 1 to n bit numbers with no consecutive 1s in binary representation?
- Count Substrings with equal number of 0s, 1s and 2s in C++
- Count ways to reach the n’th stair
- Count ways to express a number as sum of consecutive numbers in C++
- Count number of trailing zeros in (1^1)*(2^2)*(3^3)*(4^4)*.. in C++
- Sum of the series 1 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + … + upto n terms in C++
- Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n) in C++
- Count number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1<=a<=b<=c<= n in C++
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2 in C++

Advertisements