2591. Distribute Money to Maximum Children LeetCode Solution
In this guide, you will get 2591. Distribute Money to Maximum Children LeetCode Solution with the best time and space complexity. The solution to Distribute Money to Maximum Children problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Distribute Money to Maximum Children solution in C++
- Distribute Money to Maximum Children solution in Java
- Distribute Money to Maximum Children solution in Python
- Additional Resources

Problem Statement of Distribute Money to Maximum Children
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.
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 <= 200
2 <= children <= 30
Complexity Analysis
- Time Complexity: O(1)
- Space Complexity: O(1)
2591. Distribute Money to Maximum Children LeetCode Solution in C++
class Solution {
public:
int distMoney(int money, int children) {
// Everyone must receive at least 1 dollar.
money -= children;
if (money < 0)
return -1;
const int count7 = money / 7;
const int remaining = money % 7;
// Distribute 8 dollars to every child.
if (count7 == children && remaining == 0)
return count7;
// Need to move 1 dollar from the last child with 4 dollars to one of other
// children. That's why we need to substract 1.
if (count7 == children - 1 && remaining == 3)
return count7 - 1;
// Though there might be child with 4 dollars, since count7 < children - 1,
// we have "extra" spot to move money to if needed.
return min(children - 1, count7);
}
};
/* code provided by PROGIEZ */
2591. Distribute Money to Maximum Children LeetCode Solution in Java
class Solution {
public int distMoney(int money, int children) {
// Everyone must receive at least 1 dollar.
money -= children;
if (money < 0)
return -1;
final int count7 = money / 7;
final int remaining = money % 7;
// Distribute 8 dollars to every child.
if (count7 == children && remaining == 0)
return count7;
// Need to move 1 dollar from the last child with 4 dollars to one of other
// children. That's why we need to substract 1.
if (count7 == children - 1 && remaining == 3)
return count7 - 1;
// Though there might be child with 4 dollars, since count7 < children - 1,
// we have "extra" spot to move money to if needed.
return Math.min(children - 1, count7);
}
}
// code provided by PROGIEZ
2591. Distribute Money to Maximum Children LeetCode Solution in Python
class Solution:
def distMoney(self, money: int, children: int) -> int:
# Everyone must receive at least 1 dollar.
money -= children
if money < 0:
return -1
count7 = money // 7
remaining = money % 7
# Distribute 8 dollars to every child.
if count7 == children and remaining == 0:
return count7
# Need to move 1 dollar from the last child with 4 dollars to one of other
# children. That's why we need to substract 1.
if count7 == children - 1 and remaining == 3:
return count7 - 1
# Though there might be child with 4 dollars, since count7 < children - 1,
# we have 'extra' spot to move money to if needed.
return min(children - 1, count7)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.