Problem · Array

Fair Capped Money Distribution

Learn this problem
MediumGusto logoGustoFULLTIMEONSITE INTERVIEW

Problem statement

You have amount nonnegative integer smallest-currency units and an ordered array owed, where owed[i] is the most recipient i may receive.

Distribute money by repeatedly giving one unit, in recipient order, to each recipient who is still below what they are owed. Stop when no money remains or every recipient is fully paid. Return the paid amount for every recipient in the original order.

This round-robin definition divides payments as evenly as the caps permit, assigns residual units deterministically in input order, never exceeds an owed amount, and distributes exactly min(amount, sum(owed)) units.

Function

distributeMoney(amount: long, owed: long[]) → long[]

Examples

Example 1

amount = 40owed = [10,10,10,10]return = [10,10,10,10]

The available amount exactly covers every recipient's cap, matching the reported example.

Example 2

amount = 10owed = [2,100,100]return = [2,4,4]

The first recipient reaches its cap at 2. The remaining units are split evenly between the other two recipients.

Example 3

amount = 5owed = [10,10,10]return = [2,2,1]

One complete round gives everyone one unit. The two residual units go to the first two recipients.

Constraints

  • 0 <= amount <= 10^18.
  • 1 <= owed.length <= 100000.
  • 0 <= owed[i] <= 10^18.
  • The sum of all owed amounts fits a signed 64-bit integer.

More Gusto problems

drafts saved locally
public long[] distributeMoney(long amount, long[] owed) {
  // Write your code here.
}
amount40
owed[10,10,10,10]
expected[10,10,10,10]
checking account