• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Count number of ways to reach a given score in a game

March 16, 2015 by Dhaval Dave

Consider a game where a player can score 3 or 5 or 10 points in a move. Given a total score n, find number of ways to reach the given score.Examples:
Input: n = 20
Output: 4

There are following 4 ways to reach 20
(10, 10)
(5, 5, 10)
(5, 5, 5, 5)
(3, 3, 3, 3, 3, 5)This problem is a variation of coin change problem

Method 1 : With the help of DP and CoinChange code

int count( int S[], int m, int n )
{
    int i, j, x, y;

    // We need n+1 rows as the table is consturcted in bottom up manner using 
    // the base case 0 value case (n = 0)
    int table[n+1][m];
    
    // Fill the enteries for 0 value case (n = 0)
    for (i=0; i<m; i++)
        table[0][i] = 1;

    // Fill rest of the table enteries in bottom up manner  
    for (i = 1; i < n+1; i++)
    {
        for (j = 0; j < m; j++)
        {
            // Count of solutions including S[j]
            x = (i-S[j] >= 0)? table[i - S[j]][j]: 0;

            // Count of solutions excluding S[j]
            y = (j >= 1)? table[i][j-1]: 0;

            // total count
            table[i][j] = x + y;
        }
    }
    return table[n][m-1];
}


Method 2 :   The idea is to create a table of size n+1 to store counts of all scores from 0 to n. For every possible move (3, 5 and 10), increment values in table.

 

#include <stdio.h>

int count(int n)
{
    int table[n+1], i;

    memset(table, 0, sizeof(table));

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


You can also subscribe us for more interview questions :D 
Please do share with us at admin@gohired.in , As sharing is caring

Similar Articles

Filed Under: Amazon Interview Question, Flipkart Interview Questions, Interview Questions, problem Tagged With: Dynamic Programming, Recursion

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

SAP Off Campus Hiring_ March 2015 Computer Skills

Find the smallest window in a string containing all characters of another string

Test Cases for Round Function

Find next greater number with same set of digits

Inorder and Preorder traversals of a Binary Tree given. Output the Postorder traversal of it.

‘N’ Story Building, with 1,2,3 steps how many ways can a person reach top of building.

Implement a generic binary search algorithm for Integer Double String etc

Generate next palindrome number

Find Nearest Minimum number in left side in O(n)

Given a string, find the first character which is non-repetitive

Check if an array has duplicate numbers in O(n) time and O(1) space

Find the kth number with prime factors 3, 5 and 7

robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it

Advanced SQL Injection

CodeChef’ RRCOPY

DFS (Depth First Search)

Coin Collection Dynamic Programming

Flipkart Set 1 On Campus with Answers

Print all nodes that are at distance k from a leaf node

BlueStone E-commerce Interview Experience

How strtok() Works

Longest Increasing Subsequence

Number of Islands BFS/DFS

Interfaces in C++ (Abstract Classes in C++)

Facebook Interview Question : Interleave List

Memory Efficient LinkedList

Introduction To Number Theory ( Part 1 )

Trapping Rain Water

Reversal of LinkedList

Stickler thief

Copyright © 2025 · Genesis Framework · WordPress · Log in