Count ways to reach a score using 1 and 2 with no consecutive 2s
Last Updated :
25 Sep, 2023
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 constant auxiliary space.
First run scored could be either :
a) 1. Now the player has to score N-1 runs.
b) or 2. Since 2's can not be consecutive runs, next run scored has to be 1. After that, the player has to score N-(2+1) runs.
Below is the recursive solution of above problem.
Recursion relation would be:
CountWays(N) = CountWays(N-1) + CountWays(N-2)
Following recursive solution takes exponential time and space (similar to Fibonacci numbers).
C++
#include <iostream>
using namespace std;
int CountWays( int n)
{
if (n == 0) {
return 1;
}
if (n == 1) {
return 1;
}
if (n == 2) {
return 1 + 1;
}
return CountWays(n - 1) + CountWays(n - 3);
}
int main()
{
int n = 10;
cout << CountWays(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int CountWays( int n)
{
if (n == 0 ) {
return 1 ;
}
if (n == 1 ) {
return 1 ;
}
if (n == 2 ) {
return 1 + 1 ;
}
return CountWays(n - 1 ) + CountWays(n - 3 );
}
public static void main(String[] args)
{
int n = 5 ;
System.out.println(CountWays(n));
}
}
|
Python3
def CountWays(n):
if n = = 0 :
return 1
if n = = 1 :
return 1
if n = = 2 :
return 1 + 1
return CountWays(n - 1 ) + CountWays(n - 3 )
if __name__ = = '__main__' :
n = 5
print (CountWays(n))
|
C#
using System;
class GFG {
static int CountWays( int n)
{
if (n == 0) {
return 1;
}
if (n == 1) {
return 1;
}
if (n == 2) {
return 1 + 1;
}
return CountWays(n - 1) + CountWays(n - 3);
}
static public void Main()
{
int n = 5;
Console.WriteLine(CountWays(n));
}
}
|
Javascript
<script>
function CountWays(n, flag)
{
if (n == 0) {
return 1;
}
if (n == 1) {
return 1;
}
if (n == 2) {
return 1 + 1;
}
return CountWays(n - 1) + CountWays(n - 3);
}
let n = 5;
document.write(CountWays(n, false ));
</script>
|
PHP
<?php
function CountWays( $n )
{
if ( $n == 0) {
return 1;
}
if ( $n == 1) {
return 1;
}
if ( $n == 2) {
return 1 + 1;
}
return CountWays( $n - 1) + CountWays( $n - 3);
}
$n = 5;
echo CountWays( $n );
?>
|
We can store intermediate values and solve it in O(n) time and O(n) space.
C++
#include <iostream>
using namespace std;
int CountWays( int n)
{
int noOfWays[n + 3];
noOfWays[0] = 1;
noOfWays[1] = 1;
noOfWays[2] = 1 + 1;
for ( int i=3; i<n+1; i++) {
noOfWays[i] =
noOfWays[i-1]
+ noOfWays[i-3];
}
return noOfWays[n];
}
int main()
{
int n = 0;
cout << CountWays(n);
return 0;
}
|
Java
import java.util.Arrays;
class GfG {
static int CountWays( int n)
{
int noOfWays[] = new int [n + 3 ];
noOfWays[ 0 ] = 1 ;
noOfWays[ 1 ] = 1 ;
noOfWays[ 2 ] = 1 + 1 ;
for ( int i = 3 ; i < n + 1 ; i++) {
noOfWays[i] =
noOfWays[i - 1 ]
+ noOfWays[i - 3 ];
}
return noOfWays[n];
}
public static void main(String[] args)
{
int n = 5 ;
System.out.println(CountWays(n));
}
}
|
Python3
def CountWays(n):
noOfWays = [ 0 ] * (n + 3 )
noOfWays[ 0 ] = 1
noOfWays[ 1 ] = 1
noOfWays[ 2 ] = 1 + 1
for i in range ( 3 , n + 1 ):
noOfWays[i] = noOfWays[i - 1 ] + noOfWays[i - 3 ]
return noOfWays[n]
if __name__ = = '__main__' :
n = 5
print (CountWays(n))
|
C#
using System;
class GfG
{
static int CountWays( int n)
{
if (dp[n, flag] != -1)
{
return dp[n, flag];
}
if (n == 0)
{
return 1;
}
int sum = 0;
if (flag == 0 && n > 1)
{
sum = sum + CountWays(n - 1, 0) +
CountWays(n - 2, 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 <dp.GetLength(0); i++)
for ( int j = 0; j < dp.GetLength(1); j++)
dp[i,j]=-1;
Console.WriteLine(CountWays(n, 0));
}
}
|
Javascript
<script>
function CountWays(n)
{
var noOfWays = Array(n + 3).fill(0);
noOfWays[0] = 1;
noOfWays[1] = 1;
noOfWays[2] = 1 + 1;
for ( var i = 3; i < n + 1; i++) {
noOfWays[i] = noOfWays[i - 1] + noOfWays[i - 3];
}
return noOfWays[n];
}
var n = 5;
document.write(CountWays(n));
</script>
|
This can be further improved to O(n) time and constant space by storing only last 3 values.
C++
#include <iostream>
using namespace std;
int CountWays( int n)
{
int noOfWays[3];
noOfWays[0] = 1;
noOfWays[1] = 1;
noOfWays[2] = 1 + 1;
for ( int i=3; i<n+1; i++) {
noOfWays[i] =
noOfWays[3-1]
+ noOfWays[3-3];
noOfWays[0] = noOfWays[1];
noOfWays[1] = noOfWays[2];
noOfWays[2] = noOfWays[i];
}
return noOfWays[n];
}
int main()
{
int n = 5;
cout << CountWays(n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int CountWays( int n)
{
int noOfWays[] = new int [n + 3 ];
noOfWays[ 0 ] = 1 ;
noOfWays[ 1 ] = 1 ;
noOfWays[ 2 ] = 1 + 1 ;
for ( int i= 3 ; i<n+ 1 ; i++) {
noOfWays[i] =
noOfWays[ 3 - 1 ]
+ noOfWays[ 3 - 3 ];
noOfWays[ 0 ] = noOfWays[ 1 ];
noOfWays[ 1 ] = noOfWays[ 2 ];
noOfWays[ 2 ] = noOfWays[i];
}
return noOfWays[n];
}
public static void main (String[] args) {
int n = 5 ;
System.out.println(CountWays(n));
}
}
|
Python3
def CountWays(n):
noOfWays = [ 0 ] * (n + 1 )
noOfWays[ 0 ] = 1
noOfWays[ 1 ] = 1
noOfWays[ 2 ] = 1 + 1
for i in range ( 3 , n + 1 ):
noOfWays[i] = noOfWays[ 3 - 1 ] + noOfWays[ 3 - 3 ]
noOfWays[ 0 ] = noOfWays[ 1 ]
noOfWays[ 1 ] = noOfWays[ 2 ]
noOfWays[ 2 ] = noOfWays[i]
return noOfWays[n]
if __name__ = = '__main__' :
n = 5
print (CountWays(n))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static int CountWays( int n) {
int []noOfWays = new int [n + 3];
noOfWays[0] = 1;
noOfWays[1] = 1;
noOfWays[2] = 1 + 1;
for ( int i = 3; i < n + 1; i++) {
noOfWays[i] =
noOfWays[3 - 1]
+ noOfWays[3 - 3];
noOfWays[0] = noOfWays[1];
noOfWays[1] = noOfWays[2];
noOfWays[2] = noOfWays[i];
}
return noOfWays[n];
}
public static void Main(String[] args) {
int n = 5;
Console.WriteLine(CountWays(n));
}
}
|
Javascript
<script>
function CountWays(n)
{
var noOfWays = Array(3).fill(0);
noOfWays[0] = 1;
noOfWays[1] = 1;
noOfWays[2] = 1 + 1;
for ( var i = 3; i < n + 1; i++) {
noOfWays[i] =
noOfWays[3-1]
+ noOfWays[3-3];
noOfWays[0] = noOfWays[1];
noOfWays[1] = noOfWays[2];
noOfWays[2] = noOfWays[i];
}
return noOfWays[n];
}
var n = 5;
document.write(CountWays(n));
</script>
|
Approach#4: Using dynamic programming
This approach uses a dynamic programming approach to count the number of ways to reach a score of n using 1’s and 2’s such that no two consecutive 2’s are present. It uses three variables a, b, and c to store the number of ways to reach the scores i-2, i-1, and i, respectively. The loop iterates through all scores from 2 to n and updates the variables accordingly. The final count is stored in the variable c.
Algorithm
1. Initialize variables a and b to 1 and 1, respectively.
2. Initialize variable c to 0.
3. For i from 2 to n, set c to a+b.
4. Set a to b.
5. Set b to c.
6. If the last score was 2, subtract the count of ways for score n-3 from c.
7. Return c.
C++
#include <iostream>
using namespace std;
int count_ways_to_reach_score( int n) {
int a = 1;
int b = 1;
int c = 0;
for ( int i = 2; i <= n; i++) {
c = a + b;
a = b;
b = c;
if (i >= 3) {
c -= a;
}
}
return c*2;
}
int main() {
int n = 4;
cout << count_ways_to_reach_score(n) << endl;
n = 5;
cout << count_ways_to_reach_score(n) << endl;
return 0;
}
|
Java
class GFG {
public static int countWaysToReachScore( int n) {
int a = 1 ;
int b = 1 ;
int c = 0 ;
for ( int i = 2 ; i <= n; i++) {
c = a + b;
a = b;
b = c;
if (i >= 3 ) {
c -= a;
}
}
return c * 2 ;
}
public static void main(String[] args) {
int n = 4 ;
System.out.println(countWaysToReachScore(n));
n = 5 ;
System.out.println(countWaysToReachScore(n));
}
}
|
Python3
def count_ways_to_reach_score(n):
a = 1
b = 1
c = 0
for i in range ( 2 , n + 1 ):
c = a + b
a = b
b = c
if i > = 3 :
c - = a
return c * 2
n = 4
print (count_ways_to_reach_score(n))
n = 5
print (count_ways_to_reach_score(n))
|
C#
using System;
class GFG {
static int CountWaysToReachScore( int n)
{
int a = 1;
int b = 1;
int c = 0;
for ( int i = 2; i <= n; i++) {
c = a + b;
a = b;
b = c;
if (i >= 3) {
c -= a;
}
}
return c
* 2;
}
static void Main()
{
int n = 4;
Console.WriteLine(CountWaysToReachScore(n));
n = 5;
Console.WriteLine(CountWaysToReachScore(n));
}
}
|
Javascript
function countWaysToReachScore(n) {
let a = 1;
let b = 1;
let c = 0;
for (let i = 2; i <= n; i++) {
c = a + b;
a = b;
b = c;
if (i >= 3) {
c -= a;
}
}
return c * 2;
}
function main() {
let n = 4;
console.log(countWaysToReachScore(n));
n = 5;
console.log(countWaysToReachScore(n));
}
main();
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...