AlgoLounge

Dice Ways

medium

Count the number of ordered outcomes produced by rolling k standard dice (faces 1-6) that sum to a target value. Each distinct sequence of rolls counts as a separate way.

Examples

Example 1

Input:  k = 2, target = 7
Output: 6

Ways to get sum 7 with 2 dice: (1,6), (2,5), (3,4), (4,3), (5,2), (6,1)

Example 2

Input:  k = 1, target = 3
Output: 1

Only way is rolling a 3

Example 3

Input:  k = 2, target = 1
Output: 0

Impossible (minimum sum with 2 dice is 2)

Constraints

  • 1 ≤ k ≤ 30
  • 1 ≤ target ≤ 1000
  • Return 0 if impossible (target < k or target > 6k)
TypeScript
Loading editor…
List of test cases (10)
Case 1
k = 2
target = 7
Case 2
k = 1
target = 3
Case 3
k = 2
target = 1
Case 4
k = 3
target = 10
Case 5
k = 1
target = 6
Case 6
k = 2
target = 12
Case 7
k = 2
target = 2
Case 8
k = 3
target = 3
Case 9
k = 2
target = 6
Case 10
k = 4
target = 14