Problem · Array

Distribute Packages

Learn this problem
MediumAmazonOA
See Amazon hiring insights

Problem statement

Amazon has to distribute multiple packages across all of their delivery trucks. Given an array of trucks where trucks[i] represents the ith truck quantity. We also have another input to_distribute which states the number of packages we want to distribute across all the trucks. So how would we distribute the to_distribute number such that the max total of each truck is minimized?

Function

distributePackages(trucks: int[], to_distribute: int) → int[]

Complete the function distributePackages in the editor.

distributePackages has the following parameters:

  1. 1. int[] trucks: an array of integers representing the quantity of each truck
  2. 2. int to_distribute: the number of packages to distribute

Returns

int[]: an array representing the distributed packages across the trucks

Examples

Example 1

trucks = [2, 3, 4, 5, 6]to_distribute = 10return = [6, 6, 6, 6, 6]
This means that 4 packages go to the first truck, 3 packages to the second truck, 2 packages to the third truck, 1 package to the fourth truck, and none to the fifth truck, resulting in each truck having a total of 6 packages.

Constraints

🍇🍇

More Amazon problems

drafts saved locally
public int[] distributePackages(int[] trucks, int to_distribute) {
  // write your code here
}
trucks[2, 3, 4, 5, 6]
to_distribute10
expected[6, 6, 6, 6, 6]
checking account