Build an app to help grandparents divide their giving budget among their grandchildren. Write a program that calculates the number of lucky gifts (equal to 8) based on the budget and the number of grandchildren.
How it works
Many rules, mixing tradition and superstition, govern this gift.
- Gifts must not contain the amount
4, as this sounds like “death”. - It is auspicious to give an amount of
8, as this sounds like “fortune”. - It would be wrong to give nothing to any of the grandchildren.
So your algorithm should return the number of gifts equal to following these rules:
- Spend the entire budget, unless there is enough budget to give
8to everyone. - Do not give any
4; by tradition, the budget will never be4. - Do not give any
0, unless there is not enough money for all giftees. - Give a maximum amount of
8once the above rules are met.
Examples
- Classic budget:
money = 11,giftees = 2. One lucky grandchild could get8and the other could get3, so the function must return1. - Perfect budget:
money = 32,giftees = 4. It is possible to give8to all the grandchildren, so we must return4. - Budget with a four:
money = 12,giftees = 2. If the first grandchild gets8then the second would get4, which is forbidden. Whatever the choice (7and5, or6and6), nobody will get an amount of8, so we must return0.
This problem asks for a greedy allocation under special cultural rules: give a total budget to a number of grandchildren while maximizing how many receive exactly 8. If the budget can cover 8 for everyone, the answer is simply the number of grandchildren. Otherwise, the allocation must avoid any gift of 4 and, unless impossible, avoid zeros as well. The examples illustrate the logic: 11 split across 2 grandchildren can yield one gift of 8, 32 across 4 grandchildren can yield four gifts of 8, and 12 across 2 grandchildren cannot produce any 8 without violating the rules.