Skip to main content
Back to problems
#2591
Easy Algorithms

Distribute money to maximum children

Math Greedy
20.5% acceptance
Feb 25, 2026
363
924
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.

Solution

Rust
Time O(1)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn dist_money(money: i32, children: i32) -> i32 {
    // All money must be distributed. Everyone gets at least 1$. Nobody gets 4$.
    // Maximize number of children getting exactly 8$.
    if money < children {
      return -1;
    }
    let mut remaining = money - children; // extra dollars after giving 1 to each
    let mut max_eights = (remaining / 7).min(children);
    remaining -= 7 * max_eights;
    let children_left = children - max_eights;
    if children_left == 0 && remaining > 0 {
      // All children would get 8$, but remaining > 0 forces one to get more than 8$
      max_eights -= 1;
    } else if children_left == 1 && remaining == 3 {
      // Last child would get 1 + 3 = 4$, which is forbidden
      max_eights -= 1;
    }
    max_eights
  }
}