MICROSOFT Coding Question β Solved
A foundry in Hackerland makes an alloy out of n different metals. In the manufacturing of an alloy, the composition of each metal is fixed, where the required quantity of the i-th metal in preparing 1 unit of the alloy is denoted by composition[i]. The company already has stock[i] units of metal i in their stock. The company has a budget to purchase any of the metals if needed. The cost of the i-th metal is cost[i] per unit. Find the maximum units of alloys the company can produce by using available stock plus what they can purchase within their budget.
Note: Their supplier has an infinite supply of all metal types.
Example:
There are n = 2 metal types.
- their required composition = [1, 2]
- their available stock = [0, 1]
- their purchase cost = [1, 1]
- their budget = 3
A maximum of 1 unit of alloy can be produced.
- The cost for 1 unit: required quantity = [1, 2], stock = [0, 1], extra metal requirements = [1, 1], cost = (1 x 1) + (1 x 1) = 2, which is within the budget.
- The cost for 2 units: required quantity = [2, 4], stock = [0, 1], extra metal requirements = [2, 3], cost = (2 x 1) + (3 x 1) = 5, which is beyond the budget.
The answer is 1.
Function Description:
Complete the function findMaximumAlloyUnits in the editor below.
findMaximumAlloyUnits has the following parameters:
int composition[n]: the composition of metals in 1 unit of alloy
int stock[n]: the units of metals type i that the company has in stock
int cost[n]: the costs of metals type i.
int budget: the total money the company can spend
Returns:
int: the maximum unit of alloys that can be produced
Constraints:
1 β€ n β€ 10^5
1 β€ budget β€ 10^9
1 β€ composition[i] β€ 10^5
1 β€ stock[i] β€ 10^5
1 β€ cost[i] β€ 10^5
composition[i] * cost[i] β€ 2 * 10^9
Input Format For Custom Testing:
The first line contains an integer, n, the number of elements in array composition.
Each line i of the next n subsequent lines (where 0 β€ i < n) contains an integer, composition[i].
The next line contains an integer, n, the number of elements in array stock.
Each line i of the next n subsequent lines (where 0 β€ i < n) contains an integer, stock[i].
The next line contains an integer, n, the number of elements in array cost.
Each line i of the next n subsequent lines (where 0 β€ i < n) contains an integer, cost[i].
The next line contains an integer, budget.
Asked in:
MICROSOFT