Distribute Money to Maximum Children
EasyUpdated: Aug 2, 2025
Practice on:
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
1dollar. - Nobody receives
4dollars.
Return _themaximum 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.
Examples
Example 1
Input: money = 20, children = 3
Output: 1
Explanation:
The maximum number of children with 8 dollars will be 1. One of the ways to distribute the money is:
- 8 dollars to the first child.
- 9 dollars to the second child.
- 3 dollars to the third child.
It can be proven that no distribution exists such that number of children getting 8 dollars is greater than 1.
Example 2
Input: money = 16, children = 2
Output: 2
Explanation: Each child can be given 8 dollars.
Constraints
1 <= money <= 2002 <= children <= 30
Solution
Method 1 – Greedy Distribution
Intuition
To maximize the number of children who get exactly 8 dollars, we should give as many children as possible exactly 8 dollars, while ensuring that:
- Each child gets at least 1 dollar.
- No child gets exactly 4 dollars.
- All money is distributed. We need to be careful with the remaining money after giving 8 dollars to as many children as possible, to avoid the forbidden 4-dollar case.
Approach
- If there is not enough money to give each child at least 1 dollar, return -1.
- Let
max8 = (money - children) // 7(since each 8-dollar child needs 7 extra dollars after the initial 1 dollar). - The answer cannot exceed the number of children, so
max8 = min(max8, children). - For each possible number of 8-dollar children from
max8down to 0:- Compute the remaining money after giving 8 dollars to
kchildren and 1 dollar to the rest. - If the remaining money is not 4 (to avoid the forbidden case), and the remaining money is not negative, return
k.
- Compute the remaining money after giving 8 dollars to
- If no valid distribution is found, return -1.
Code
C++
class Solution {
public:
int distMoney(int money, int children) {
if (money < children) return -1;
int max8 = min((money - children) / 7, children);
for (int k = max8; k >= 0; --k) {
int left = money - k * 8;
int rest = children - k;
if (rest == 0 && left == 0) return k;
if (rest == 0) continue;
if (left >= rest && left != 4 * rest) return k;
}
return -1;
}
};
Go
func distMoney(money int, children int) int {
if money < children {
return -1
}
max8 := (money - children) / 7
if max8 > children {
max8 = children
}
for k := max8; k >= 0; k-- {
left := money - k*8
rest := children - k
if rest == 0 && left == 0 {
return k
}
if rest == 0 {
continue
}
if left >= rest && left != 4*rest {
return k
}
}
return -1
}
Java
class Solution {
public int distMoney(int money, int children) {
if (money < children) return -1;
int max8 = Math.min((money - children) / 7, children);
for (int k = max8; k >= 0; --k) {
int left = money - k * 8;
int rest = children - k;
if (rest == 0 && left == 0) return k;
if (rest == 0) continue;
if (left >= rest && left != 4 * rest) return k;
}
return -1;
}
}
Kotlin
class Solution {
fun distMoney(money: Int, children: Int): Int {
if (money < children) return -1
var max8 = (money - children) / 7
if (max8 > children) max8 = children
for (k in max8 downTo 0) {
val left = money - k * 8
val rest = children - k
if (rest == 0 && left == 0) return k
if (rest == 0) continue
if (left >= rest && left != 4 * rest) return k
}
return -1
}
}
Python
class Solution:
def distMoney(self, money: int, children: int) -> int:
if money < children:
return -1
max8 = min((money - children) // 7, children)
for k in range(max8, -1, -1):
left = money - k * 8
rest = children - k
if rest == 0 and left == 0:
return k
if rest == 0:
continue
if left >= rest and left != 4 * rest:
return k
return -1
Rust
impl Solution {
pub fn dist_money(money: i32, children: i32) -> i32 {
if money < children {
return -1;
}
let mut max8 = (money - children) / 7;
if max8 > children {
max8 = children;
}
for k in (0..=max8).rev() {
let left = money - k * 8;
let rest = children - k;
if rest == 0 && left == 0 {
return k;
}
if rest == 0 {
continue;
}
if left >= rest && left != 4 * rest {
return k;
}
}
-1
}
}
TypeScript
class Solution {
distMoney(money: number, children: number): number {
if (money < children) return -1;
let max8 = Math.min(Math.floor((money - children) / 7), children);
for (let k = max8; k >= 0; --k) {
const left = money - k * 8;
const rest = children - k;
if (rest === 0 && left === 0) return k;
if (rest === 0) continue;
if (left >= rest && left !== 4 * rest) return k;
}
return -1;
}
}
Complexity
- ⏰ Time complexity:
O(children), since we try at mostchildrenpossible values for k. - 🧺 Space complexity:
O(1), only a constant amount of extra space is used.