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 _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

1
2
3
4
5
6
7
8
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

1
2
3
Input: money = 16, children = 2
Output: 2
Explanation: Each child can be given 8 dollars.

Constraints

  • 1 <= money <= 200
  • 2 <= 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

  1. If there is not enough money to give each child at least 1 dollar, return -1.
  2. Let max8 = (money - children) // 7 (since each 8-dollar child needs 7 extra dollars after the initial 1 dollar).
  3. The answer cannot exceed the number of children, so max8 = min(max8, children).
  4. For each possible number of 8-dollar children from max8 down to 0:
    • Compute the remaining money after giving 8 dollars to k children 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.
  5. If no valid distribution is found, return -1.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
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;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
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;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
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
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
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
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
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 most children possible values for k.
  • 🧺 Space complexity: O(1), only a constant amount of extra space is used.