MICROSOFT Coding Question – Solved

11 Live
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

Image of the Question

Question Image Question Image Question Image Question Image Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


def findMaximumAlloyUnits(composition, stock, cost, budget):
    
    def solve(mid):
        total_cost = 0
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To solve this problem, the goal is to find the maximum number of alloy units that can be produced given the metal composition per unit, available stock of metals, cost per unit for purchasing metals, and a budget constraint. The challenge lies in efficiently determining how many alloy units can be made using both existing stock and additional metals bought within the budget.

Here’s how you can approach the problem step-by-step:

1. **Understand the problem fully:**
Each unit of alloy requires a fixed quantity of each metal type as given by the `composition` array. The company already has some stock of these metals. For making a certain number of alloy units, you will need some metals from the stock and may need to buy more if stock is insufficient. Each unit of metal purchased has an associated cost. The budget limits the total cost spent on buying metals.

2. **Translate the requirements into a cost function:**
Suppose you want to produce `x` units of alloy.
- For each metal type i, the total metal required is `x * composition[i]`.
- The stock available for that metal is `stock[i]`.
- If `x * composition[i] > stock[i]`, then the extra amount to buy is `(x * composition[i] - stock[i])`.
- If `stock[i]` suffices for that metal, no extra purchase is needed.
The total purchase cost for metal i is `cost[i] * max(0, x * composition[i] - stock[i])`.
Summing these costs over all metals gives the total cost to produce `x` units.

3. **Goal: Find the maximum `x` such that total cost ≤ budget.**
The problem essentially reduces to a decision problem: given an `x`, can we afford to produce `x` units within the budget?
If yes, try a larger `x`. If no, try a smaller `x`.

4. **Brute force is not feasible:**
Since `n` can be as large as 10^5, and `budget` can be as high as 10^9, iterating through all possible values of `x` to find the maximum is inefficient. You need a more optimized approach.

5. **Use binary search on `x`:**
- Set low = 0, high = a very large number (like the maximum possible units if budget was unlimited).
- While low ≤ high:
- Calculate mid = (low + high) // 2.
- Compute total cost required to produce `mid` units using the formula above.
- If total cost ≤ budget, it means producing `mid` units is possible; move low to mid + 1 to check if more units can be produced.
- Else, reduce high to mid - 1.
The answer will be `high` at the end, representing the maximum feasible units.

6. **Efficiently compute the total cost for a given `x`:**
For each metal, calculate the required quantity and compare it with stock.
- Compute extra = max(0, x * composition[i] - stock[i]).
- Compute cost contribution = extra * cost[i].
Sum these costs for all metals.
Since `n` can be large, this summation should be done efficiently, which is straightforward in a single pass.

7. **Handle potential overflow:**
Since `composition[i]`, `cost[i]`, and `x` can be large, the multiplication can exceed typical integer ranges. Use appropriate data types (like 64-bit integers) to avoid overflow.
Also, remember that the problem guarantees `composition[i] * cost[i] ≤ 2 * 10^9`, which helps in reasoning about bounds.

8. **Edge cases to consider:**
- When `x` = 0, cost should be zero and obviously feasible.
- When the stock already suffices to produce some units without any purchase, test that scenario.
- When the budget is zero, the maximum units are limited by stock only.
- Metals that have zero composition for the alloy (if any) should be handled correctly (they require no metal).

9. **Summary of approach:**
- Use binary search on the number of units `x`.
- For each `x`, calculate the total cost to buy metals that are insufficient in stock.
- Adjust binary search bounds based on affordability.
- Return the maximum `x` for which the total cost fits in the budget.

10. **Why binary search works:**
The total cost function is monotonically non-decreasing with respect to `x`: producing more units never costs less. This monotonicity allows binary search to efficiently pinpoint the maximum `x`.

By applying this binary search approach combined with a cost calculation function, you can find the maximum units of alloy producible under budget constraints efficiently and correctly.
```


Related Questions