Number of Burgers with No Waste of Ingredients - Problem

Given two integers tomatoSlices and cheeseSlices, determine how many burgers can be made with no waste of ingredients.

The ingredients required for different burgers are:

  • Jumbo Burger: 4 tomato slices and 1 cheese slice
  • Small Burger: 2 tomato slices and 1 cheese slice

Return [total_jumbo, total_small] such that the number of remaining tomatoSlices equals 0 and the number of remaining cheeseSlices equals 0. If it is not possible to achieve zero waste, return [].

Input & Output

Example 1 — Basic Case
$ Input: tomatoSlices = 16, cheeseSlices = 7
Output: [1,6]
💡 Note: 1 jumbo burger (4 tomatoes + 1 cheese) + 6 small burgers (12 tomatoes + 6 cheese) = 16 tomatoes + 7 cheese total
Example 2 — Only Small Burgers
$ Input: tomatoSlices = 10, cheeseSlices = 5
Output: [0,5]
💡 Note: 0 jumbo burgers + 5 small burgers (10 tomatoes + 5 cheese) uses all ingredients perfectly
Example 3 — Impossible Case
$ Input: tomatoSlices = 4, cheeseSlices = 17
Output: []
💡 Note: Not enough tomatoes to satisfy minimum requirement (2 tomatoes per cheese slice). 4 < 2×17 = 34

Constraints

  • 0 ≤ tomatoSlices ≤ 107
  • 0 ≤ cheeseSlices ≤ 107

Visualization

Tap to expand
Burger Problem - No Waste Solution INPUT x16 x7 tomatoSlices = 16 cheeseSlices = 7 Burger Types: JUMBO 4T + 1C SMALL 2T + 1C T=Tomato, C=Cheese ALGORITHM STEPS 1 Set Up Equations 4J + 2S = 16 (tomatoes) J + S = 7 (cheese) 2 Solve for Jumbo J = (T - 2*C) / 2 J = (16 - 14) / 2 = 1 3 Solve for Small S = C - J S = 7 - 1 = 6 4 Verify No Waste Tomatoes: 4*1 + 2*6 = 16 Cheese: 1 + 6 = 7 Validation: OK J >= 0 and S >= 0 Time: O(1) | Space: O(1) FINAL RESULT Burgers Made: 1 Jumbo 6 Small Output Array: [1, 6] Ingredient Usage: Tomatoes: 16/16 Cheese: 7/7 Zero Waste: OK Key Insight: This is a system of 2 linear equations with 2 unknowns. Using substitution: Jumbo = (T - 2*C) / 2 and Small = C - J. Valid solution exists only when: T is even, T >= 2*C, and T <= 4*C. This ensures non-negative integer solutions. TutorialsPoint - Number of Burgers with No Waste of Ingredients | Optimal Solution
Asked in
Google 12 Facebook 8 Amazon 6
32.5K Views
Medium Frequency
~15 min Avg. Time
892 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