Maximize Eight-Dollar Shares without Giving Anyone Four

Quick Overview

Distribute all money while giving each child at least one dollar and never four, maximizing the number who receive exactly eight.

Maximize Eight-Dollar Shares without Giving Anyone Four

Company: MongoDB

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Distribute all `money` dollars among `children` children. Each child must receive at least one dollar, and no child may receive exactly four dollars. Return the maximum number of children who can receive exactly eight dollars. Return `-1` if no valid distribution exists. ```cpp class Solution { public: int distMoney(int money, int children); }; ``` ### Examples **Example 1:** ```text Input: money = 10, children = 3 Output: 1 Explanation: Give the children 8, 1, and 1 dollars. Two eight-dollar shares already exceed the total. ``` **Example 2:** ```text Input: money = 12, children = 2 Output: 0 Explanation: Giving one child 8 would leave the forbidden amount 4 for the other. Giving 6 to each child is valid. ``` ### Constraints - `1 <= money <= 200`. - `2 <= children <= 30`. ### Clarifying Questions - Can money be left over? No; all must be distributed. - May a child get more than eight dollars? Yes. - Is four dollars forbidden even when nobody gets eight? Yes. - Does an answer of zero mean impossible? No; it means a valid distribution exists but none can give a child exactly eight. ```hint Check the boundary Check the difference between leaving money undistributed and giving a child more than eight dollars. ```

Overview: Distribute all money while giving each child at least one dollar and never four, maximizing the number who receive exactly eight.

MongoDB logo
MongoDB
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
1
0

Problem

Distribute all money dollars among children children. Each child must receive at least one dollar, and no child may receive exactly four dollars. Return the maximum number of children who can receive exactly eight dollars. Return -1 if no valid distribution exists.

class Solution {
public:
    int distMoney(int money, int children);
};

Examples

Example 1:

Input: money = 10, children = 3
Output: 1
Explanation: Give the children 8, 1, and 1 dollars. Two eight-dollar shares already exceed the total.

Example 2:

Input: money = 12, children = 2
Output: 0
Explanation: Giving one child 8 would leave the forbidden amount 4 for the other. Giving 6 to each child is valid.

Constraints

  • 1 <= money <= 200 .
  • 2 <= children <= 30 .

Clarifying Questions Guidance

  • Can money be left over? No; all must be distributed.
  • May a child get more than eight dollars? Yes.
  • Is four dollars forbidden even when nobody gets eight? Yes.
  • Does an answer of zero mean impossible? No; it means a valid distribution exists but none can give a child exactly eight.