Consider a game where players can score 3, 5, or 10 points in a move. Given a total score of N, The task is to find the number of ways to reach the given score.
Examples:
Input: n = 20
Output: 4
Explanation: There are following 4 ways to reach 20: (10, 10), (5, 5, 10), (5, 5, 5, 5), (3, 3, 3, 3, 3, 5)
Input: n = 13
Output: 2
Explanation: There are following 2 ways to reach 13: (3, 5, 5), (3, 10)
The Given problem is a variation of coin change problem.
Finding the Number of Ways By Using the Bottom Up Dynamic Programming:
Using the Bottom up Dynamic Programming Approach to find the Number of Ways to reach the Given score.
Follow the below steps to Implement the idea:
- Create an array table[] of size N+1 to store counts of all scores from 0 to N.
- For every possible move (3, 5, and 10), increment the number of ways to reach the current score x i.e. table[x] with ways in which those scores can be reached from where x is reachable i.e. (x – 3), (x – 5), (x – 10).
- Return table[N] .
Below is the Implementation of the above approach:
C++
#include <iostream>
using namespace std;
int count( int n)
{
int table[n + 1], i;
for ( int j = 0; j < n + 1; j++)
table[j] = 0;
table[0] = 1;
for (i = 3; i <= n; i++)
table[i] += table[i - 3];
for (i = 5; i <= n; i++)
table[i] += table[i - 5];
for (i = 10; i <= n; i++)
table[i] += table[i - 10];
return table[n];
}
int main( void )
{
int n = 20;
cout << "Count for " << n << " is " << count(n) << endl;
n = 13;
cout << "Count for " << n << " is " << count(n) << endl;
return 0;
}
|
C
#include <stdio.h>
int count( int n)
{
int table[n + 1], i;
for ( int i = 0; i < n + 1; i++) {
table[i] = 0;
}
table[0] = 1;
for (i = 3; i <= n; i++)
table[i] += table[i - 3];
for (i = 5; i <= n; i++)
table[i] += table[i - 5];
for (i = 10; i <= n; i++)
table[i] += table[i - 10];
return table[n];
}
int main( void )
{
int n = 20;
printf ( "Count for %d is %d\n" , n, count(n));
n = 13;
printf ( "Count for %d is %d" , n, count(n));
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
static int count( int n)
{
int table[] = new int [n + 1 ], i;
Arrays.fill(table, 0 );
table[ 0 ] = 1 ;
for (i = 3 ; i <= n; i++)
table[i] += table[i - 3 ];
for (i = 5 ; i <= n; i++)
table[i] += table[i - 5 ];
for (i = 10 ; i <= n; i++)
table[i] += table[i - 10 ];
return table[n];
}
public static void main(String[] args)
{
int n = 20 ;
System.out.println( "Count for " + n + " is "
+ count(n));
n = 13 ;
System.out.println( "Count for " + n + " is "
+ count(n));
}
}
|
Python3
def count(n):
table = [ 0 for i in range (n + 1 )]
table[ 0 ] = 1
for i in range ( 3 , n + 1 ):
table[i] + = table[i - 3 ]
for i in range ( 5 , n + 1 ):
table[i] + = table[i - 5 ]
for i in range ( 10 , n + 1 ):
table[i] + = table[i - 10 ]
return table[n]
n = 20
print ( 'Count for' , n, 'is' , count(n))
n = 13
print ( 'Count for' , n, 'is' , count(n))
|
C#
using System;
class GFG {
static int count( int n)
{
int [] table = new int [n + 1];
for ( int j = 0; j < n + 1; j++)
table[j] = 0;
table[0] = 1;
for ( int i = 3; i <= n; i++)
table[i] += table[i - 3];
for ( int i = 5; i <= n; i++)
table[i] += table[i - 5];
for ( int i = 10; i <= n; i++)
table[i] += table[i - 10];
return table[n];
}
public static void Main()
{
int n = 20;
Console.WriteLine( "Count for " + n + " is "
+ count(n));
n = 13;
Console.Write( "Count for " + n + " is " + count(n));
}
}
|
PHP
<?php
function counts( $n )
{
for ( $j = 0; $j < $n + 1; $j ++)
$table [ $j ] = 0;
$table [0] = 1;
for ( $i = 3; $i <= $n ; $i ++)
$table [ $i ] += $table [ $i - 3];
for ( $i = 5; $i <= $n ; $i ++)
$table [ $i ] += $table [ $i - 5];
for ( $i = 10; $i <= $n ; $i ++)
$table [ $i ] += $table [ $i - 10];
return $table [ $n ];
}
$n = 20;
echo "Count for " ;
echo ( $n );
echo ( " is " );
echo counts( $n );
$n = 13;
echo ( "\n" ) ;
echo "Count for " ;
echo ( $n );
echo ( " is " );
echo counts( $n );
?>
|
Javascript
<script>
function count(n)
{
let table = new Array(n + 1), i;
for (let j = 0; j < n + 1; j++)
table[j] = 0;
table[0] = 1;
for (i = 3; i <= n; i++)
table[i] += table[i - 3];
for (i = 5; i <= n; i++)
table[i] += table[i - 5];
for (i = 10; i <= n; i++)
table[i] += table[i - 10];
return table[n];
}
let n = 20;
document.write( "Count for " + n
+ " is " + count(n) + "<br>" );
n = 13;
document.write( "Count for " + n + " is "
+ count(n) + "<br>" );
</script>
|
OutputCount for 20 is 4
Count for 13 is 2
Time Complexity: O(N), Where N is the score to be obtained.
Auxiliary Space: O(N), Where N is the score to be obtained.
Another Approach:
- Define a function countWays that takes an integer parameter n representing the total score to be obtained and returns an integer representing the number of ways to reach the given score.
- Create a vector ways of size n + 1 to store the counts of all scores from 0 to n. Initialize all the values of the vector to 0.
- Set the base case: If the given value n is 0, set ways[0] to 1, as there is one way to obtain 0 score, i.e., by not making any moves.
- Define an integer array moves of size 3, which contains all the possible moves, i.e., 3, 5, and 10.
- Using nested loops, iterate over all the possible moves and scores that can be obtained using those moves. Starting from the moves[i] score to the total score n, update the ways[j] value with the sum of ways[j – moves[i]] and the current value of ways[j]. This is because the number of ways to obtain the score j using the move moves[i] is equal to the sum of the number of ways to obtain the score j – moves[i] using the same move and the current number of ways to obtain the score j using all the possible moves.
- Return the value of ways[n], which represents the number of ways to obtain the given score n using the given moves.
- Define the main function that calls the countWays function for two different values of n, i.e., 20 and 13, and print the number of ways obtained using the given moves for each value of n.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <vector>
using namespace std;
int countWays( int n)
{
vector< int > ways(n + 1, 0);
ways[0] = 1;
int moves[] = {3, 5, 10};
for ( int i = 0; i < 3; i++) {
for ( int j = moves[i]; j <= n; j++) {
ways[j] += ways[j - moves[i]];
}
}
return ways[n];
}
int main( void )
{
int n = 20;
cout << "Count for " << n << " is " << countWays(n) << endl;
n = 13;
cout << "Count for " << n << " is " << countWays(n) << endl;
return 0;
}
|
Java
import java.util.*;
public class ScoreCount {
static int countWays( int n)
{
int [] ways = new int [n + 1 ];
ways[ 0 ] = 1 ;
int [] moves = { 3 , 5 , 10 };
for ( int i = 0 ; i < 3 ; i++) {
for ( int j = moves[i]; j <= n; j++) {
ways[j] += ways[j - moves[i]];
}
}
return ways[n];
}
public static void main(String[] args)
{
int n = 20 ;
System.out.println( "Count for " + n + " is "
+ countWays(n));
n = 13 ;
System.out.println( "Count for " + n + " is "
+ countWays(n));
}
}
|
Python3
def countWays(n: int ) - > int :
ways = [ 0 ] * (n + 1 )
ways[ 0 ] = 1
moves = [ 3 , 5 , 10 ]
for i in range ( 3 ):
for j in range (moves[i], n + 1 ):
ways[j] + = ways[j - moves[i]]
return ways[n]
n = 20
print (f "Count for {n} is {countWays(n)}" )
n = 13
print (f "Count for {n} is {countWays(n)}" )
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
public static int countWays( int n)
{
List< int > ways = new List< int >();
for ( int i = 0; i <= n; i++)
ways.Add(0);
ways[0] = 1;
int [] moves = { 3, 5, 10 };
for ( int i = 0; i < 3; i++)
{
for ( int j = moves[i]; j <= n; j++)
{
ways[j] += ways[j - moves[i]];
}
}
return ways[n];
}
public static void Main( string [] args)
{
int n = 20;
Console.WriteLine( "Count for {0} is {1}" , n, countWays(n));
n = 13;
Console.WriteLine( "Count for {0} is {1}" , n, countWays(n));
}
}
|
Javascript
function countWays(n) {
const ways = new Array(n + 1).fill(0);
ways[0] = 1;
const moves = [3, 5, 10];
for (let i = 0; i < 3; i++) {
for (let j = moves[i]; j <= n; j++) {
ways[j] += ways[j - moves[i]];
}
}
return ways[n];
}
let n = 20;
console.log(`Count for ${n} is ${countWays(n)}`);
n = 13;
console.log(`Count for ${n} is ${countWays(n)}`);
|
OutputCount for 20 is 4
Count for 13 is 2
Time Complexity: O(N)
Auxiliary Space: O(N)
Exercise: How to count score when (10, 5, 5), (5, 5, 10) and (5, 10, 5) are considered as different sequences of moves. Similarly, (5, 3, 3), (3, 5, 3) and (3, 3, 5) are considered different.
This article is contributed by Rajeev Arora. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above