Check Whether Two Custom Dice Give a Flat Sum Distribution
Arrays & HashingAuto-graded · 24 test casesLast updated
Decide whether two lists of die-face labels make every total from 1 to 12 appear exactly three times across all 36 face pairings, using pair counting.
Two six-sided dice are rolled together. Each die is described by a list holding the integer written on each of its faces. Labels may repeat, and they may be zero or negative.
Every face of each die is equally likely, so there are 36 equally likely (face of die X, face of die Y) pairings. Each pairing produces a total: the label on X plus the label on Y.
The pair of dice is flat if every total from 1 through 12 is produced by exactly 3 of the 36 pairings. That makes each of those totals equally likely, with probability 1/12, and no other total can occur.
Write a function that takes the two label lists and returns True if the pair is flat, and False otherwise.
Details
- Count pairings, not distinct label values. Two faces with the same label are separate faces and count separately.
- A die must have exactly six faces. If either list does not have length 6, return
False. - The order of the faces within a list does not matter.
- Return a Python
bool.
Constraints
- Each list holds integers. The labels can be zero, negative, or repeated.
- Lists may have any length, including empty, but only length exactly 6 can give
True. - Label magnitudes are small enough that sums fit in ordinary integers.
- You only need to check the given pair. You do not need to find other label sets.
Examples
Example 1
Input: is_flat_sum_pair([1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6])
Output: False
Example 2
Input: is_flat_sum_pair([0, 1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6])
Output: False
Example 3
Input: is_flat_sum_pair([1, 1, 1, 4, 5, 6], [1, 2, 3, 4, 5, 6])
Output: False
Example 4
Input: is_flat_sum_pair([1, 1, 2, 2, 3, 3], [4, 4, 5, 5, 6, 6])
Output: False
Hints
Hint 1
Six faces times six faces gives 36 pairings. If 12 totals share them equally, how many pairings must produce each total?
Hint 2
Build a frequency table of all 36 sums, using two nested loops over the faces and a counter keyed by the sum.
Hint 3
Check each total from 1 to 12 for a count of exactly 3. Once those 12 counts are 3 each, they use up all 36 pairings, so no other total can appear. Remember to reject lists whose length is not 6.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Two six-faced dice give 36 equally likely pairings. For the totals 1 through 12 to be equally likely, each must come from 36 / 12 = 3 pairings.
- If either list does not have exactly 6 entries, return
False. - Enumerate all 36 pairings and count how often each sum occurs, using a hash map.
- Return
Trueonly if each total from 1 to 12 has a count of exactly 3.
Why it is correct
The 12 required totals, each with count 3, account for 12 x 3 = 36 pairings. That is every pairing there is, so no pairing can produce a total outside 1 to 12. Checking just those 12 counts is therefore enough. Counting pairings, not distinct labels, correctly handles repeated labels. A total that never occurs has count 0 in the Counter, so it fails the check.
Complexity
- Time: O(1) for fixed size dice, since there are 36 pairings. In general it is O(n * m) for lists of lengths n and m, though the length check cuts off every other case immediately.
- Space: O(1), since there are at most 36 distinct sums.
Reference solution
from collections import Counter
from typing import List
def is_flat_sum_pair(die_x: List[int], die_y: List[int]) -> bool:
if len(die_x) != 6 or len(die_y) != 6:
return False
counts = Counter(a + b for a in die_x for b in die_y)
return all(counts[s] == 3 for s in range(1, 13))