Related Articles

# Count ways to reach a score using 1 and 2 with no consecutive 2s

• Difficulty Level : Medium
• Last Updated : 13 May, 2021

A cricket player has to score N runs, with condition he can take either 1 or 2 runs only and consecutive runs should not be 2. Find all the possible combination he can take.
Examples:

```Input : N = 4
Output : 4
1+1+1+1, 1+2+1, 2+1+1, 1+1+2

Input : N = 5
Output : 6```

Source :Oracle Interview On campus

This problem is a variation of count number of ways to reach given score in a game and can be solved in O(n) time and O(n) auxiliary space.
Below is the recursive solution of above problem.

## C++

 `// A simple recursive implementation for``// counting ways to reach a score using 1 and 2 with``// consecutive 2 allowed``#include ``using` `namespace` `std;` `int` `CountWays(``int` `n, ``bool` `flag)``{``    ``if` `(n == 0) ``// base case``        ``return` `1;` `    ``int` `sum = 0;` `    ``// 2 is not scored last time so we can score either 2 or 1``    ``if` `(flag == ``false` `&& n > 1)``        ``sum = sum + CountWays(n - 1, ``false``) + CountWays(n - 2, ``true``);` `    ``// 2 is scored last time so we can only score 1``    ``else``        ``sum = sum + CountWays(n - 1, ``false``);` `    ``return` `sum;``}` `// Driver code``int` `main()``{``    ``int` `n = 5;``    ``cout << CountWays(n, ``false``);``    ``return` `0;``}`

## Java

 `// A simple recursive implementation for``// counting ways to reach a score using 1 and 2 with``// consecutive 2 allowed` `import` `java.io.*;` `class` `GFG {``   ``static`  `int` `CountWays(``int` `n, ``boolean` `flag)``{``    ``if` `(n == ``0``) ``// base case``        ``return` `1``;` `    ``int` `sum = ``0``;` `    ``// 2 is not scored last time so we can score either 2 or 1``    ``if` `(flag == ``false` `&& n > ``1``)``        ``sum = sum + CountWays(n - ``1``, ``false``) + CountWays(n - ``2``, ``true``);` `    ``// 2 is scored last time so we can only score 1``    ``else``        ``sum = sum + CountWays(n - ``1``, ``false``);` `    ``return` `sum;``}``    ``// Driver code``    ``public` `static` `void` `main (String[] args) {``            ``int` `n = ``5``;``            ``System.out.println(CountWays(n, ``false``));``    ``}``}`

## Python3

 `# A simple recursive implementation for``# counting ways to reach a score using 1 and 2 with``# consecutive 2 allowed` `def` `CountWays(n,flag):` `    ``# base case``    ``if` `n ``=``=``0``:``        ``return` `1``    ``sum` `=``0``    ``# 2 is not scored last time so``    ``# we can score either 2 or 1``    ``if` `flag ``=``=``False` `and` `n>``1``:``        ``sum` `=` `sum``+``CountWays(n``-``1``,``False``)``+``CountWays(n``-``2``,``True``)``    ``else``:``    ``# 2 is scored last time so we can only score 1``        ``sum` `=` `sum``+``CountWays(n``-``1``,``False``)``    ``return` `sum` `# Driver code``if` `__name__``=``=``'__main__'``:``    ``n ``=` `5``    ``print``(CountWays(n,``False``))` `# This code is contributed by``# Shrikant13`

## C#

 `// A simple recursive implementation``// for counting ways to reach a score``// using 1 and 2 with consecutive 2 allowed``using` `System;` `class` `GFG``{``static` `int` `CountWays(``int` `n, ``bool` `flag)``{``    ``if` `(n == 0) ``// base case``        ``return` `1;` `    ``int` `sum = 0;` `    ``// 2 is not scored last time so``    ``// we can score either 2 or 1``    ``if` `(flag == ``false` `&& n > 1)``        ``sum = sum + CountWays(n - 1, ``false``) +``                    ``CountWays(n - 2, ``true``);` `    ``// 2 is scored last time so``    ``// we can only score 1``    ``else``        ``sum = sum + CountWays(n - 1, ``false``);` `    ``return` `sum;``}` `// Driver Code``static` `public` `void` `Main ()``{``    ``int` `n = 5;``    ``Console.WriteLine(CountWays(n, ``false``));``}``}` `// This code is contributed by Sach_Code`

## PHP

 ` 1)``        ``\$sum` `= ``\$sum` `+ CountWays(``\$n` `- 1, false) +``                      ``CountWays(``\$n` `- 2, true);` `    ``// 2 is scored last time so``    ``// we can only score 1``    ``else``        ``\$sum` `= ``\$sum` `+ CountWays(``\$n` `- 1, false);` `    ``return` `\$sum``;``}` `// Driver Code``\$n` `= 5;``echo` `CountWays(``\$n``, false);` `// This code is contributed``// by Sach_Code``?>`

## Javascript

 ``
Output:
`6`

This problem has optimal substructure property as the problem can be solved using solutions to subproblems.
Below is the Dp solution of above problem

## C++

 `// A memoization based implementation for``// counting ways to reach a score using 1 and 2 with``// consecutive 2 allowed``#include ``using` `namespace` `std;` `const` `int` `MAX = 101;``int` `dp[MAX];` `int` `CountWays(``int` `n, ``int` `flag = 0)``{``    ``// if this state is already visited return``    ``// its value``    ``if` `(dp[n][flag] != -1)``        ``return` `dp[n][flag];` `    ``// base case``    ``if` `(n == 0)``        ``return` `1;` `    ``// 2 is not scored last time so we can``    ``// score either 2 or 1``    ``int` `sum = 0;``    ``if` `(flag == 0 && n > 1)``        ``sum = sum + CountWays(n - 1, 0) + CountWays(n - 2, 1);` `    ``// 2 is scored last time so we can only score 1``    ``else``        ``sum = sum + CountWays(n - 1, 0);` `    ``return` `dp[n][flag] = sum;``}` `int` `main()``{``    ``int` `n = 5;``    ``memset``(dp, -1, ``sizeof``(dp));``    ``cout << CountWays(n);``    ``return` `0;``}`

## Java

 `import` `java.util.Arrays;` `// A memoization based implementation for``// counting ways to reach a score using``// 1 and 2 with consecutive 2 allowed``class` `GfG``{` `    ``static` `int` `MAX = ``101``;``    ``static` `int` `dp[][] = ``new` `int``[MAX][``2``];` `    ``static` `int` `CountWays(``int` `n, ``int` `flag)``    ``{``        ``// if this state is already visited return``        ``// its value``        ``if` `(dp[n][flag] != -``1``)``        ``{``            ``return` `dp[n][flag];``        ``}` `        ``// base case``        ``if` `(n == ``0``)``        ``{``            ``return` `1``;``        ``}` `        ``// 2 is not scored last time so we can``        ``// score either 2 or 1``        ``int` `sum = ``0``;``        ``if` `(flag == ``0` `&& n > ``1``)``        ``{``            ``sum = sum + CountWays(n - ``1``, ``0``) +``                    ``CountWays(n - ``2``, ``1``);``        ``}``        ` `        ``// 2 is scored last time so we can only score 1``        ``else``        ``{``            ``sum = sum + CountWays(n - ``1``, ``0``);``        ``}` `        ``return` `dp[n][flag] = sum;``    ``}` `    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `n = ``5``;``        ``for` `(``int` `i = ``0``; i < MAX; i++)``        ``{``            ``Arrays.fill(dp[i], -``1``);``        ``}``        ``System.out.println(CountWays(n, ``0``));``    ``}``}` `/* This code contributed by PrinciRaj1992 */`

## Python3

 `# A memoization based implementation for``# counting ways to reach a score using``# 1 and 2 with consecutive 2 allowed` `MAX` `=` `101``dp ``=` `[[``-``1` `for` `i ``in` `range``(``2``)]``          ``for` `i ``in` `range``(``MAX``)]` `def` `CountWays(n, flag):``    ` `    ``# if this state is already visited``    ``# return its value``    ``if` `(dp[n][flag] !``=` `-``1``):``        ``return` `dp[n][flag]` `    ``# base case``    ``if` `(n ``=``=` `0``):``        ``return` `1` `    ``# 2 is not scored last time so``    ``# we can score either 2 or 1``    ``sum` `=` `0``    ``if` `(flag ``=``=` `0` `and` `n > ``1``):``        ``sum` `=` `(``sum` `+` `CountWays(n ``-` `1``, ``0``) ``+``                     ``CountWays(n ``-` `2``, ``1``))` `    ``# 2 is scored last time so we``    ``# can only score 1``    ``else``:``        ``sum` `=` `sum` `+` `CountWays(n ``-` `1``, ``0``)` `    ``dp[n][flag] ``=` `sum``    ``return` `dp[n][flag]` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ``n ``=` `5``    ``print``(CountWays(n, ``0``))` `# This code is contributed by``# Surendra_Gangwar`

## C#

 `// A memoization based implementation for``// counting ways to reach a score using``// 1 and 2 with consecutive 2 allowed``using` `System;` `class` `GfG``{` `    ``static` `int` `MAX = 101;``    ``static` `int` `[,]dp = ``new` `int``[MAX, 2];` `    ``static` `int` `CountWays(``int` `n, ``int` `flag)``    ``{``        ``// if this state is already visited return``        ``// its value``        ``if` `(dp[n, flag] != -1)``        ``{``            ``return` `dp[n, flag];``        ``}` `        ``// base case``        ``if` `(n == 0)``        ``{``            ``return` `1;``        ``}` `        ``// 2 is not scored last time so we can``        ``// score either 2 or 1``        ``int` `sum = 0;``        ``if` `(flag == 0 && n > 1)``        ``{``            ``sum = sum + CountWays(n - 1, 0) +``                    ``CountWays(n - 2, 1);``        ``}``        ` `        ``// 2 is scored last time so we can only score 1``        ``else``        ``{``            ``sum = sum + CountWays(n - 1, 0);``        ``}` `        ``return` `dp[n,flag] = sum;``    ``}` `    ``// Driver code``    ``public` `static` `void` `Main(String[] args)``    ``{``        ``int` `n = 5;``        ``for` `(``int` `i = 0; i

## Javascript

 ``
Output:
`6`

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up