Count Ordered Dice Outcomes With a Given Total

Quick Overview

Count Ordered Dice Outcomes With a Given Total. Roll n dice, each with faces1 through k. Count ordered outcomes whose sum equals target, and return the count modulo1000000007.

Count Ordered Dice Outcomes With a Given Total

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Roll n dice, each with faces 1 through k. Count ordered outcomes whose sum equals target, and return the count modulo 1000000007. Different choices for individual dice count separately even when they give the same sum. ```cpp class Solution { public: int numRollsToTarget(int n, int k, int target); }; ``` ### Examples **Example 1:** ```text Input: n = 2, k = 3, target = 4 Output: 3 Explanation: The outcomes are(1,3),(2,2),(3,1). ``` **Example 2:** ```text Input: n = 3, k = 2, target = 4 Output: 3 Explanation: Exactly one of the three dice shows2 and the other two show1. ``` ### Constraints - Both n and k are integers in `[1,30]`. - target is an integer between 1 and 1000. ### Contract considerations Dice are distinct positions in an ordered outcome. No unordered-partition interpretation is used. ```hint Read the boundary condition Does swapping the results of two different dice create a different outcome, even when the total is unchanged? ``` ### Boundary reminders Dice are distinct positions in an ordered outcome. No unordered-partition interpretation is used.

Overview: Count Ordered Dice Outcomes With a Given Total. Roll n dice, each with faces1 through k. Count ordered outcomes whose sum equals target, and return the count modulo1000000007.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Roll n dice, each with faces 1 through k. Count ordered outcomes whose sum equals target, and return the count modulo 1000000007. Different choices for individual dice count separately even when they give the same sum.

class Solution {
public:
    int numRollsToTarget(int n, int k, int target);
};

Examples

Example 1:

Input: n = 2, k = 3, target = 4
Output: 3
Explanation: The outcomes are(1,3),(2,2),(3,1).

Example 2:

Input: n = 3, k = 2, target = 4
Output: 3
Explanation: Exactly one of the three dice shows2 and the other two show1.

Constraints

  • Both n and k are integers in [1,30] .
  • target is an integer between 1 and 1000.

Contract considerations

Dice are distinct positions in an ordered outcome. No unordered-partition interpretation is used.

Boundary reminders

Dice are distinct positions in an ordered outcome. No unordered-partition interpretation is used.