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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// A simple recursive implementation for
// counting ways to reach a score using 1 and 2 with
// consecutive 2 allowed
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// A simple recursive implementation 
// for counting ways to reach a score 
// using 1 and 2 with consecutive 2 allowed 
  
function CountWays($n, $flag
    if ($n == 0) // base case 
        return 1; 
  
    $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
$n = 5; 
echo CountWays($n, false); 
  
// This code is contributed 
// by Sach_Code
?>

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// A memoization based implementation for
// counting ways to reach a score using 1 and 2 with
// consecutive 2 allowed
#include <bits/stdc++.h>
using namespace std;
  
const int MAX = 101;
int dp[MAX][2];
  
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;
}

chevron_right


Output:

6


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : shrikanth13, Sach_Code