Number of Burgers with No Waste of Ingredients
MediumUpdated: Aug 2, 2025
Practice on:
Problem
Given two integers tomatoSlices and cheeseSlices. The ingredients of different burgers are as follows:
- Jumbo Burger:
4tomato slices and1cheese slice. - Small Burger:
2Tomato slices and1cheese slice.
Return [total_jumbo, total_small] so that the number of remaining
tomatoSlices equal to 0 and the number of remaining cheeseSlices equal to 0. If it is not possible to make the remaining tomatoSlices and
cheeseSlices equal to 0 return [].
Examples
Example 1
Input: tomatoSlices = 16, cheeseSlices = 7
Output: [1,6]
**Explantion:** To make one jumbo burger and 6 small burgers we need 4*1 + 2*6 = 16 tomato and 1 + 6 = 7 cheese.
There will be no remaining ingredients.
Example 2
Input: tomatoSlices = 17, cheeseSlices = 4
Output: []
**Explantion:** There will be no way to use all ingredients to make small and jumbo burgers.
Example 3
Input: tomatoSlices = 4, cheeseSlices = 17
Output: []
**Explantion:** Making 1 jumbo burger there will be 16 cheese remaining and making 2 small burgers there will be 15 cheese remaining.
Constraints
0 <= tomatoSlices, cheeseSlices <= 10^7
Solution
Method 1 – Math, Linear Equations
Intuition
Let x = jumbo burgers, y = small burgers. Solve: 4x + 2y = tomatoSlices x + y = cheeseSlices Express x and y in terms of tomatoSlices and cheeseSlices, check for integer and non-negative solutions.
Approach
- x = tomatoSlices/2 - cheeseSlices
- y = 2*cheeseSlices - tomatoSlices/2
- Both x and y must be >= 0 and integer, tomatoSlices must be even.
- Return [x, y] or [] if impossible.
Code
C++
#include <vector>
using namespace std;
class Solution {
public:
vector<int> numOfBurgers(int tomatoSlices, int cheeseSlices) {
if (tomatoSlices%2) return {};
int x = tomatoSlices/2 - cheeseSlices;
int y = cheeseSlices - x;
if (x < 0 || y < 0) return {};
return {x, y};
}
};
Go
func numOfBurgers(tomatoSlices, cheeseSlices int) []int {
if tomatoSlices%2 != 0 { return []int{} }
x := tomatoSlices/2 - cheeseSlices
y := cheeseSlices - x
if x < 0 || y < 0 { return []int{} }
return []int{x, y}
}
Java
import java.util.*;
class Solution {
public List<Integer> numOfBurgers(int tomatoSlices, int cheeseSlices) {
if (tomatoSlices%2 != 0) return new ArrayList<>();
int x = tomatoSlices/2 - cheeseSlices;
int y = cheeseSlices - x;
if (x < 0 || y < 0) return new ArrayList<>();
return Arrays.asList(x, y);
}
}
Kotlin
class Solution {
fun numOfBurgers(tomatoSlices: Int, cheeseSlices: Int): List<Int> {
if (tomatoSlices%2 != 0) return emptyList()
val x = tomatoSlices/2 - cheeseSlices
val y = cheeseSlices - x
if (x < 0 || y < 0) return emptyList()
return listOf(x, y)
}
}
Python
class Solution:
def numOfBurgers(self, tomatoSlices: int, cheeseSlices: int) -> List[int]:
if tomatoSlices%2 != 0:
return []
x = tomatoSlices//2 - cheeseSlices
y = cheeseSlices - x
if x < 0 or y < 0:
return []
return [x, y]
Rust
impl Solution {
pub fn num_of_burgers(tomato_slices: i32, cheese_slices: i32) -> Vec<i32> {
if tomato_slices%2 != 0 { return vec![]; }
let x = tomato_slices/2 - cheese_slices;
let y = cheese_slices - x;
if x < 0 || y < 0 { return vec![]; }
vec![x, y]
}
}
TypeScript
function numOfBurgers(tomatoSlices: number, cheeseSlices: number): number[] {
if (tomatoSlices%2 !== 0) return [];
const x = tomatoSlices/2 - cheeseSlices;
const y = cheeseSlices - x;
if (x < 0 || y < 0) return [];
return [x, y];
}
Complexity
- ⏰ Time complexity:
O(1) - 🧺 Space complexity:
O(1)