Maximum Number of Alloys - Problem

You are the owner of a company that creates alloys using various types of metals. There are n different types of metals available, and you have access to k machines that can be used to create alloys.

Each machine requires a specific amount of each metal type to create an alloy. For the i-th machine to create an alloy, it needs composition[i][j] units of metal of type j.

Initially, you have stock[i] units of metal type i, and purchasing one unit of metal type i costs cost[i] coins.

Given integers n, k, budget, a 2D array composition, and arrays stock and cost, your goal is to maximize the number of alloys the company can create while staying within the budget of budget coins.

All alloys must be created with the same machine.

Return the maximum number of alloys that the company can create.

Input & Output

Example 1 — Basic Manufacturing
$ Input: n = 3, k = 2, budget = 15, composition = [[1,1,1],[1,1,2]], stock = [0,0,0], cost = [1,2,3]
Output: 2
💡 Note: Machine 0 needs [1,1,1] per alloy: cost = 1×1 + 1×2 + 1×3 = 6 per alloy. With budget 15, we can make 15÷6 = 2 alloys. Machine 1 needs [1,1,2] per alloy: cost = 1×1 + 1×2 + 2×3 = 9 per alloy. With budget 15, we can make 15÷9 = 1 alloy. Maximum is 2.
Example 2 — With Initial Stock
$ Input: n = 3, k = 2, budget = 15, composition = [[1,1,1],[1,1,2]], stock = [1,1,0], cost = [1,2,3]
Output: 3
💡 Note: Machine 0: For 3 alloys need [3,3,3]. Have [1,1,0], need to buy [2,2,3]. Cost = 2×1 + 2×2 + 3×3 = 15 ≤ 15. For 4 alloys need [4,4,4], buy [3,3,4]. Cost = 3×1 + 3×2 + 4×3 = 21 > 15. Machine 1: For 2 alloys need [2,2,4], buy [1,1,4]. Cost = 1×1 + 1×2 + 4×3 = 15 ≤ 15. For 3 alloys need [3,3,6], buy [2,2,6]. Cost = 2×1 + 2×2 + 6×3 = 24 > 15. Maximum is 3.
Example 3 — Single Machine
$ Input: n = 2, k = 1, budget = 10, composition = [[2,1]], stock = [1,1], cost = [1,1]
Output: 5
💡 Note: Only machine 0: needs [2,1] per alloy. For 5 alloys need [10,5]. Have [1,1], need to buy [9,4]. Cost = 9×1 + 4×1 = 13 > 10. For 4 alloys need [8,4], buy [7,3]. Cost = 7×1 + 3×1 = 10 ≤ 10. So maximum is 4 alloys.

Constraints

  • 1 ≤ n, k ≤ 100
  • 0 ≤ budget ≤ 108
  • composition[i][j] ≥ 1
  • 0 ≤ stock[i] ≤ 108
  • 1 ≤ cost[i] ≤ 100

Visualization

Tap to expand
Maximum Number of Alloys INPUT n = 3, k = 2, budget = 15 Composition Matrix: Machine 1: 1 1 1 Machine 2: 1 1 2 Stock: 0 0 0 Cost: 1 2 3 Machine 1 [1,1,1] Machine 2 [1,1,2] M1 M2 M3 ALGORITHM STEPS 1 Binary Search Setup Search range: [1, budget + max(stock)] 2 For Each Machine Calculate cost for mid alloys 3 Cost Calculation need = max(0, comp[j]*mid - stock[j]) total += need * cost[j] 4 Binary Search Decision If cost <= budget: left = mid + 1 Else: right = mid - 1 Machine 1: Try 2 alloys Metal 1: 1*2 - 0 = 2 units * 1 = 2 Metal 2: 1*2 - 0 = 2 units * 2 = 4 Metal 3: 1*2 - 0 = 2 units * 3 = 6 Total: 2+4+6 = 12 <= 15 OK 3 alloys: 3+6+9 = 18 > 15 NO FINAL RESULT Machine Analysis: Machine 1: [1,1,1] 2 alloys cost: 12 coins Max alloys: 2 (within budget) Machine 2: [1,1,2] 2 alloys cost: 14 coins Max alloys: 2 (within budget) Maximum Alloys: 2 Using Machine 1 Cost: 12 coins Budget left: 3 coins Key Insight: Binary search is optimal because the cost function is monotonic - more alloys always cost more. For each machine, we binary search the max alloys achievable within budget. Time: O(k * n * log(budget)). Try each machine independently and return the global maximum across all machines. TutorialsPoint - Maximum Number of Alloys | Binary Search Approach
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
18.5K Views
Medium Frequency
~25 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen