Problem · Array

Allocate Workers for a Production Ratio

Learn this problem
EasyBarclays logoBarclaysFULLTIMEOA

Problem statement

A factory must produce two products in a fixed quantity ratio. You are given positive integers total, firstRatio, secondRatio, firstRate, and secondRate.

  • The factory must produce exactly total items.
  • The first and second product quantities must have ratio firstRatio : secondRatio.
  • One first-product worker produces firstRate items per day.
  • One second-product worker produces secondRate items per day.

The ratio divides total exactly, and each product quantity is divisible by its corresponding per-worker rate. Return a two-element integer array containing the required first-product worker count followed by the required second-product worker count.

Function

allocateWorkers(total: int, firstRatio: int, secondRatio: int, firstRate: int, secondRate: int) → int[]

Examples

Example 1

total = 2160firstRatio = 2secondRatio = 1firstRate = 18secondRate = 24return = [80,30]

The ratio has three total parts, so the product quantities are 2160 * 2 / 3 = 1440 and 2160 * 1 / 3 = 720. The required worker counts are 1440 / 18 = 80 and 720 / 24 = 30.

Example 2

total = 1000firstRatio = 3secondRatio = 2firstRate = 20secondRate = 25return = [30,16]

The product quantities are 600 and 400. Dividing by the corresponding daily rates gives 30 and 16 workers.

Example 3

total = 630firstRatio = 1secondRatio = 2firstRate = 7secondRate = 14return = [30,30]

The two product quantities are 210 and 420. Although the quantities differ, the rates make both worker counts equal to 30.

Constraints

  • 1 <= total, firstRatio, secondRatio, firstRate, secondRate <= 10^9.
  • firstRatio + secondRatio <= 2 * 10^9.
  • total * firstRatio and total * secondRatio fit in a signed 64-bit integer.
  • total * firstRatio and total * secondRatio are each divisible by firstRatio + secondRatio.
  • Each resulting product quantity is divisible by its corresponding per-worker rate.
drafts saved locally
public int[] allocateWorkers(int total, int firstRatio, int secondRatio, int firstRate, int secondRate) {
    // Write your code here.
}
total2160
firstRatio2
secondRatio1
firstRate18
secondRate24
expected[80,30]
checking account