Talentd
FleetCode
Make and follow sheets.Sign in

Distribute Money to Maximum Children solution and explanation

EasyGreedyMath6 min read20.5% acceptanceUpdated 9 Apr 2026
Distribute Money to Maximum Children || Greedy Algorithm || Leetcode-2591Aryan Mittal · 11:19 · 1K viewsWatch on YouTube
9 more videos on this problem
  1. Distribute Money to Maximum Children || Greedy Algorithm || Leetcode-2591Aryan Mittal · 11:19 · 1K views
  2. A. Distribute Money to Maximum Children - LEETCODE BIWEEKLY CONTEST 100 (Logic explained in details)Joyjit Codes · 4:55 · 1K views
  3. Distribute Money to Maximum Children || leetcode Biweekly 100 || Leetcode EasyBinaryMagic · 9:54 · 1K views
  4. LeetCode#2591 Distribute Money to Maximum Children - PythonCodeJulian · 3:00 · 1K views
  5. Leetcode 2591: Distribute Money to Maximum ChildrenAlgorithms Casts · 11:15 · 670 views
  6. Distribute Money to Maximum Children | #6323 || Leetcode | Biweekly contest 100 || EnglishVishwas aithal · 7:16 · 592 views
  7. 2591. Distribute Money to Maximum Children (Leetcode Easy)Programming Live with Larry · 18:22 · 511 views
  8. Zendesk Interview Question - Distribute Money to Maximum Children - Leetcode 2591- PythonSephorus · 7:19 · 461 views
  9. 2591. Distribute Money to Maximum Children (Leetcode Biweekly Contest 100)Let's Do It · 7:30 · 431 views
  10. Distribute Money to Maximum Children | Leetcode 2591 | Math | C++ | BiWeekly 100Tech Courses · 10:30 · 368 views

The 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.

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.

def max_children_with_eight(money, children):
    if money < children:
        return -1
    remaining = money - children
    max_eights = remaining // 7
    return min(max_eights, children - 1 if children else 0)

money = 20
children = 3
print(max_children_with_eight(money, children))
public class Main {
    public static int maxChildrenWithEight(int money, int children) {
        if (money < children) return -1;
        int remaining = money - children;
        int maxEights = remaining / 7;
        return Math.min(maxEights, children > 1 ? children - 1 : 0);
    }

    public static void main(String[] args) {
        int money = 20, children = 3;
        System.out.println(maxChildrenWithEight(money, children));
    }
}
#include <iostream>
using namespace std;

int maxChildrenWithEight(int money, int children) {
    if (money < children) return -1;
    int remaining = money - children;
    int maxEights = remaining / 7;
    return maxEights > children ? children - 1 : maxEights;
}

int main() {
    int money = 20, children = 3;
    cout << maxChildrenWithEight(money, children) << endl;
    return 0;
}
function maxChildrenWithEight(money, children) {
    if (money < children) return -1;
    let remaining = money - children;
    let maxEights = Math.floor(remaining / 7);
    return Math.min(maxEights, children - 1);
}

let money = 20, children = 3;
console.log(maxChildrenWithEight(money, children));
#include <stdio.h>

int maxChildrenWithEight(int money, int children) {
    if (money < children) return -1;
    int remaining = money - children;
    int maxEights = remaining / 7;
    if (maxEights > children) return children - 1;  // to avoid the case of one child with 4
    return maxEights;
}

int main() {
    int money = 20, children = 3;
    printf("%d\n", maxChildrenWithEight(money, children));
    return 0;
}
using System;

class Program {
    static int MaxChildrenWithEight(int money, int children) {
        if (money < children) return -1;
        int remaining = money - children;
        int maxEights = remaining / 7;
        return Math.Min(maxEights, children > 1 ? children - 1 : 0);
    }

    static void Main() {
        int money = 20, children = 3;
        Console.WriteLine(MaxChildrenWithEight(money, children));
    }
}

The Python implementation divides the remaining money after allocating the minimum required dollars into portions for exact eights. Edge cases are considered to ensure correctness and avoid illegal states by only using valid amounts.

Complexity

Time complexity is O(1) since it involves fixed steps of calculations. Space complexity remains O(1) as only a few variables are calculated and no large data structures are used.

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.

def brute_force_max_children_with_eight(money, children):
    max_eights = -1
    for eights in range(children + 1):
        used_money = eights * 8
        if used_money > money:
            break
        remaining_money = money - used_money
        if remaining_money >= (children - eights):
            max_eights = eights
    return max_eights

money = 20
children = 3
print(brute_force_max_children_with_eight(money, children))
public class Main {
    public static int bruteForceMaxChildrenWithEight(int money, int children) {
        int maxEights = -1;
        for (int eights = 0; eights <= children; eights++) {
            int usedMoney = eights * 8;
            if (usedMoney > money) break;
            int remainingMoney = money - usedMoney;
            if (remainingMoney >= (children - eights))
                maxEights = eights;
        }
        return maxEights;
    }

    public static void main(String[] args) {
        int money = 20, children = 3;
        System.out.println(bruteForceMaxChildrenWithEight(money, children));
    }
}
#include <iostream>
using namespace std;

int bruteForceMaxChildrenWithEight(int money, int children) {
    int maxEights = -1;
    for (int eights = 0; eights <= children; eights++) {
        int usedMoney = eights * 8;
        if (usedMoney > money) break;
        int remainingMoney = money - usedMoney;
        if (remainingMoney >= children - eights)
            maxEights = eights;
    }
    return maxEights;
}

int main() {
    int money = 20, children = 3;
    cout << bruteForceMaxChildrenWithEight(money, children) << endl;
    return 0;
}
function bruteForceMaxChildrenWithEight(money, children) {
    let maxEights = -1;
    for (let eights = 0; eights <= children; eights++) {
        let usedMoney = eights * 8;
        if (usedMoney > money) break;
        let remainingMoney = money - usedMoney;
        if (remainingMoney >= (children - eights))
            maxEights = eights;
    }
    return maxEights;
}

let money = 20,
    children = 3;
console.log(bruteForceMaxChildrenWithEight(money, children));
#include <stdio.h>

int bruteForceMaxChildrenWithEight(int money, int children) {
    int maxEights = -1;
    for (int eights = 0; eights <= children; eights++) {
        int usedMoney = eights * 8;
        if (usedMoney > money) break;
        int remainingMoney = money - usedMoney;
        if (remainingMoney >= children - eights)
            maxEights = eights;
    }
    return maxEights;
}

int main() {
    int money = 20, children = 3;
    printf("%d\n", bruteForceMaxChildrenWithEight(money, children));
    return 0;
}
using System;

class Program {
    static int BruteForceMaxChildrenWithEight(int money, int children) {
        int maxEights = -1;
        for (int eights = 0; eights <= children; eights++) {
            int usedMoney = eights * 8;
            if (usedMoney > money) break;
            int remainingMoney = money - usedMoney;
            if (remainingMoney >= (children - eights))
                maxEights = eights;
        }
        return maxEights;
    }

    static void Main() {
        int money = 20, children = 3;
        Console.WriteLine(BruteForceMaxChildrenWithEight(money, children));
    }
}

Following brute force method's reliance upon nested loops, each division trial's viability is checked. Despite inefficiencies, holistic excesses evaluation is afforded by method-specific rigor in assessing permissible proportions.

Complexity

Time complexity runs at O(children), iterating over each children possible eights allocation once. Space measures static at O(1), utilizing trivial single-value storage typical of loop implementations.

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).

function distMoney(money: number, children: number): number {
    if (money < children) {
        return -1;
    }
    if (money > 8 * children) {
        return children - 1;
    }
    if (money === 8 * children - 4) {
        return children - 2;
    }
    return Math.floor((money - children) / 7);
}
func distMoney(money int, children int) int {
	if money < children {
		return -1
	}
	if money > 8*children {
		return children - 1
	}
	if money == 8*children-4 {
		return children - 2
	}
	// money-8x >= children-x, x <= (money-children)/7
	return (money - children) / 7
}
impl Solution {
    pub fn dist_money(money: i32, children: i32) -> i32 {
        if money < children {
            return -1;
        }

        if money > children * 8 {
            return children - 1;
        }

        if money == children * 8 - 4 {
            return children - 2;
        }

        (money - children) / 7
    }
}
Try this approach in the editor

Complexity comparison

ApproachComplexity
Greedy Approach

Time complexity is O(1) since it involves fixed steps of calculations. Space complexity remains O(1) as only a few variables are calculated and no large data structures are used.

Brute Force Approach

Time complexity runs at O(children), iterating over each children possible eights allocation once. Space measures static at O(1), utilizing trivial single-value storage typical of loop implementations.

Case analysis–

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

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.

Comments

Now write it yourselfClose this page and solve Distribute Money to Maximum Children in the editor, without looking.
Practise Distribute Money to Maximum Children