Skip to main content

Distribute Money to Maximum Children - Solution & Explanation

EasyMathGreedy17 min readAsked at: Apple, Google, Gusto +1
Practice this problem

Problem Statement

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

Approach Overview

Problem Overview: You are given money dollars and children. Each child must receive at least $1. The goal is to maximize the number of children who receive exactly $8, while ensuring no child ends up with exactly $4. If distributing at least $1 to every child is impossible, return -1.

Approach 1: Brute Force Simulation (O(children) time, O(1) space)

Try every possible number of children that could receive exactly $8. For a candidate value k, compute the required money: 8 * k plus at least 1 dollar for each of the remaining children. Check whether the remaining money creates an invalid case where one child ends up with $4. Iterate from the maximum possible k down to 0 and return the first valid configuration. This approach directly models the constraints and is useful for reasoning about edge cases, but it performs multiple checks across the range of children.

Approach 2: Greedy Distribution (O(1) time, O(1) space)

The optimal solution relies on a greedy observation combined with simple math. First give every child $1. If money < children, the distribution is impossible and the answer is -1. After this base allocation, every child needs an additional $7 to reach $8. Compute how many such upgrades are possible using money / 7, capped by the number of children.

Two edge cases require adjustment. If every child receives $8 but leftover money remains, at least one child would exceed $8, so reduce the count by one. Another special case occurs when exactly one child does not receive $8 and the leftover money equals $3. That child would end up with $4 (1 + 3), which is forbidden. Reduce the count by one to avoid that configuration.

This greedy reasoning works because maximizing $8 allocations always consumes the remaining budget in chunks of $7 after the initial distribution. No additional data structures are required, making it a constant-time calculation driven purely by arithmetic constraints.

Recommended for interviews: The greedy solution is what interviewers expect. It shows you can convert problem constraints into arithmetic reasoning and handle tricky edge cases. Explaining the brute force approach first demonstrates understanding of the constraints, while the greedy optimization highlights problem-solving maturity.

Approach 1: Greedy Approach

The idea is to determine the maximum number of children that can receive exactly 8 dollars. Since each child must receive at least 1 dollar, we start with giving each child 1 dollar. This leaves us with money - children dollars. We need to maximize the number of children receiving exactly 8 dollars by ensuring that the remaining children post this allocation do not violate the rules.

In this solution, we first check if it's possible to distribute at least 1 dollar to each child. If not, return -1. Then, we calculate how much more money can be distributed after giving each child 1 dollar. By dividing this money by the additional needed to reach 8 dollars (7 more dollars needed), we determine the maximum children who can receive 8 dollars. We also ensure not exceeding the number of children to prevent robustness issues.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

The time complexity is O(1) since the operations are straightforward arithmetic calculations. The space complexity is also O(1) as no additional space is used beyond simple variables.

Try this approach in the editor →

Approach 2: Brute Force Approach

This approach evaluates all possible distributions across children to determine one that maximizes the number getting exactly 8 dollars. While inefficient for large inputs, it ensures an understanding of distribution constraints by considering every possible partition systematically.

Using a nested loop pattern, this C solution tries every combination of children and checks if their total expenditure matches the initial amount provided without breaching any specified rules. Once determined, it uses resultant combinations to find the maximum possible eights allocation. However, this approach iteratively checks all potential distributions exhaustively.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

The time complexity is O(children) as the dominant operations loop across possible combinations of children receiving 8. Space complexity is O(1), as aside from loop management variables, no additional persistent data structures are maintained.

Try this approach in the editor →

Approach 3: Case analysis

If money \lt children, then there must be a child who did not receive money, return -1.

If money \gt 8 times children, then there are children-1 children who received 8 dollars, and the remaining child received money - 8 times (children-1) dollars, return children-1.

If money = 8 times children - 4, then there are children-2 children who received 8 dollars, and the remaining two children shared the remaining 12 dollars (as long as it is not 4, 8 dollars is fine), return children-2.

If we assume that there are x children who received 8 dollars, then the remaining money is money- 8 times x, as long as it is greater than or equal to the number of remaining children children-x, it can meet the requirements. Therefore, we only need to find the maximum value of x, which is the answer.

Time complexity O(1), space complexity O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Greedy Approach

The time complexity is O(1) since the operations are straightforward arithmetic calculations. The space complexity is also O(1) as no additional space is used beyond simple variables.

Brute Force Approach

The time complexity is O(children) as the dominant operations loop across possible combinations of children receiving 8. Space complexity is O(1), as aside from loop management variables, no additional persistent data structures are maintained.

Case analysis

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force SimulationO(children)O(1)Useful for reasoning about constraints or validating edge cases during initial problem solving
Greedy DistributionO(1)O(1)Best approach for interviews and production due to constant-time math-based reasoning

Video Solution

Distribute Money to Maximum Children || Greedy Algorithm || Leetcode-2591Aryan Mittal1,973 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Distribute Money to Maximum Children easy or hard?
LeetCode classifies this problem as Easy, but the edge cases make it slightly tricky. The main difficulty is recognizing the greedy pattern and correctly handling the situation where a child would receive exactly $4. Once the constraints are converted into simple arithmetic rules, the implementation becomes straightforward.
Distribute Money to Maximum Children Python/Java solution
The greedy implementation in Python or Java follows the same steps: subtract the base allocation of $1 per child, compute how many children can receive an additional $7, and adjust the result for invalid edge cases such as leftover $3 with one remaining child. The algorithm runs in constant time and uses only a few integer variables.
How to solve Distribute Money to Maximum Children in O(1)?
Start by giving each child $1 and subtract that from the total money. Use integer division by 7 to determine how many children can reach $8. Limit that count by the total number of children, then adjust for edge cases such as leftover money causing a $4 distribution or exceeding $8 for all children.
What is the best approach for Distribute Money to Maximum Children?
The greedy approach is the best solution. First give every child $1 to satisfy the minimum constraint, then use the remaining money to upgrade children to $8 by adding $7 each. Handle special edge cases where leftover money would create a child with exactly $4 or when all children receive $8 but extra money still remains. This approach runs in O(1) time and O(1) space.
Is Distribute Money to Maximum Children asked at Google/Amazon/Meta?
This problem represents a typical greedy reasoning question commonly seen in technical interviews at large tech companies. Variations of constrained distribution and greedy optimization frequently appear in interviews at companies like Amazon, Google, and Meta to test logical reasoning and edge‑case handling.
What data structure is used in Distribute Money to Maximum Children?
No special data structure is required. The solution relies purely on arithmetic reasoning and greedy allocation. Basic integer variables track the remaining money and number of children receiving $8.
What is the time complexity of Distribute Money to Maximum Children?
The optimal greedy solution runs in O(1) time because it uses direct arithmetic calculations instead of iteration. Only a few conditional checks are required after distributing the base $1 to each child. Space complexity is also O(1) since no additional data structures are used.

Ready to solve this problem?

Practice Distribute Money to Maximum Children with our built-in code editor and test cases.

Practice on FleetCode