Distribute Money to Maximum Children - Problem
You are given an integer money denoting the amount of money (in dollars) that you have and another integer children denoting the number of children that you must distribute the money to.
You have to distribute the money according to the following rules:
- All money must be distributed.
- Everyone must receive at least 1 dollar.
- Nobody receives 4 dollars.
Return the maximum number of children who may receive exactly 8 dollars if you distribute the money according to the aforementioned rules. If there is no way to distribute the money, return -1.
Input & Output
Example 1 — Basic Case
$
Input:
money = 20, children = 3
›
Output:
1
💡 Note:
Optimal distribution is [1, 8, 11]. One child gets exactly $8. We can't give two children $8 each because that would leave $4 for the third child, which violates the constraint.
Example 2 — Impossible Case
$
Input:
money = 4, children = 2
›
Output:
-1
💡 Note:
We have $4 for 2 children. If we give each child at least $1, we need at least $2. The remaining $2 could be distributed as [1, 3] or [2, 2], but we cannot avoid giving someone exactly $4 if we try [0, 4], and $0 violates the minimum constraint.
Example 3 — All Get $8
$
Input:
money = 24, children = 3
›
Output:
3
💡 Note:
We can distribute as [8, 8, 8]. All three children get exactly $8, and no constraints are violated.
Constraints
- 1 ≤ children ≤ 50
- 1 ≤ money ≤ 200
Visualization
Tap to expand
Understanding the Visualization
1
Constraints
Everyone gets ≥$1, nobody gets $4, all money distributed
2
Strategy
Give minimum first, then maximize $8 recipients
3
Result
Maximum number of children with exactly $8
Key Takeaway
🎯 Key Insight: Give everyone the minimum $1, then use excess money to upgrade as many as possible to $8 while avoiding the forbidden $4 amount
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code