Exact Staffing for a 2:1 Nail-to-Screw Quota
Math & Bit ManipulationAuto-graded · 24 test casesLast updated
Compute the headcount of two specialist worker types so a component quota splits exactly 2:1 with every worker at full capacity, or report that no such staffing exists.
Staffing a Fastener Line
A workshop assembles products from two kinds of fasteners, nails and screws. They must be used in an exact 2 : 1 ratio: the number of nails is exactly twice the number of screws.
Each day the workshop has a target total_components, the number of nails plus the number of screws. Two kinds of specialists do the installing:
- A nail installer places exactly 500 nails per day and no screws.
- A screw installer places exactly 250 screws per day and no nails.
Every installer works a full day at exactly the stated rate. There are no partial days and no partially used installers. No other worker types exist.
Task
Given the integer total_components, find a number of nail installers a and a number of screw installers b (both integers) such that:
nails = 500 * aandscrews = 250 * b.nails + screws == total_components.nails == 2 * screws.
Output
- If such
aandbexist, return the list[a, b, a + b]. The values are unique, because the two equations fixnailsandscrews. - If no such integers exist, return
None. Never round, approximate, or choose a "closest" staffing. - If
total_componentsis0, return[0, 0, 0]. - The rule is purely arithmetic and applies to any integer input. A negative quota is not special-cased:
aandbare still the integers solving the equations above, so they may be negative. For example,-750gives[-1, -1, -2], and a negative value that has no integer solution givesNone.
Constraints
total_componentsis an integer. Do not assume it is positive or small, and use exact integer arithmetic (no floating point).- The return value is either a list of three integers or
None.
Examples
Example 1
Input: exact_staffing(10000)
Output: None
Example 2
Input: exact_staffing(300)
Output: None
Example 3
Input: exact_staffing(750)
Output: [1, 1, 2]
Example 4
Input: exact_staffing(0)
Output: [0, 0, 0]
Hints
Hint 1
If nails are twice the screws, every 3 fasteners in the quota split into 2 nails and 1 screw. What does that say about divisibility of the quota by 3?
Hint 2
Once you know the exact nail and screw counts, a staffing exists only if the nail count is a multiple of 500 and the screw count is a multiple of 250. Handle the ratio first, then convert to workers.
Hint 3
Write the quota as 3m, so nails = 2m and screws = m. Work out what both divisibility conditions imply for the quota; you should find it must be a multiple of 750, and then both worker counts are equal.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Apply the ratio first, then convert to headcount.
- A 2 : 1 ratio means the fasteners come in groups of 3 (2 nails and 1 screw). If
total_componentsis not divisible by 3, an exact split is impossible, so returnNone. - Otherwise
nails = 2 * total_components / 3andscrews = total_components / 3. - Each nail installer places exactly 500 nails and each screw installer exactly 250 screws, with no partial use. So
nailsmust be divisible by 500 andscrewsby 250. If either check fails, returnNone. - Otherwise
nail_installers = nails / 500,screw_installers = screws / 250, and the result is[nail_installers, screw_installers, their sum]. - A quota of
0gives[0, 0, 0].
Why it is correct
The ratio and the sum fix the nail and screw counts uniquely, so there is no freedom left to choose. The worker counts are then forced by the per-worker rates, and they are integers exactly when the divisibility checks pass. If a check fails, no valid staffing exists, so None is correct.
Write the quota as 3m. The nail count is 2m and the screw count is m. The conditions 500 | 2m and 250 | m both reduce to 250 | m. So a staffing exists exactly when the quota is a multiple of 750, and then both worker counts equal quota / 750 and the total is 2 * quota / 750. This is why 750 gives [1, 1, 2], while 300 and 10000 give None. The same reasoning holds for negative integers, which give negative counts, as in the statement.
Complexity
- Time: O(1), a fixed number of arithmetic operations.
- Space: O(1).
Reference solution
from typing import Optional, List
def exact_staffing(total_components: int) -> Optional[List[int]]:
if total_components == 0:
return [0, 0, 0]
# Every 3 fasteners split into 2 nails and 1 screw.
if total_components % 3 != 0:
return None
nails = (2 * total_components) // 3
screws = total_components // 3
# Installers work at full capacity only.
if nails % 500 != 0:
return None
if screws % 250 != 0:
return None
nail_installers = nails // 500
screw_installers = screws // 250
return [nail_installers, screw_installers, nail_installers + screw_installers]