Number of Burgers with No Waste of Ingredients

medium math equations

Problem

You have tomatoSlices tomato slices and cheeseSlices cheese slices. A jumbo burger needs 4 tomato slices and 1 cheese slice; a small burger needs 2 tomato slices and 1 cheese slice. Return [jumbo, small] so that every slice is used up with no leftovers, or an empty list if no such counts exist.

InputtomatoSlices = 16, cheeseSlices = 7
Output[1, 6]
1 jumbo uses 4 tomato + 1 cheese; 6 small use 12 tomato + 6 cheese. Totals: 16 tomato and 7 cheese, nothing wasted.

def num_of_burgers(tomato, cheese):
    if tomato % 2 != 0:
        return []
    jumbo = (tomato - 2 * cheese) // 2
    small = cheese - jumbo
    if jumbo < 0 or small < 0:
        return []
    return [jumbo, small]
function numOfBurgers(tomato, cheese) {
  if (tomato % 2 !== 0) return [];
  const jumbo = (tomato - 2 * cheese) / 2;
  const small = cheese - jumbo;
  if (jumbo < 0 || small < 0) return [];
  return [jumbo, small];
}
class Solution {
    public List<Integer> numOfBurgers(int tomato, int cheese) {
        if (tomato % 2 != 0) return new ArrayList<>();
        int jumbo = (tomato - 2 * cheese) / 2;
        int small = cheese - jumbo;
        if (jumbo < 0 || small < 0) return new ArrayList<>();
        return Arrays.asList(jumbo, small);
    }
}
vector<int> numOfBurgers(int tomato, int cheese) {
    if (tomato % 2 != 0) return {};
    int jumbo = (tomato - 2 * cheese) / 2;
    int small = cheese - jumbo;
    if (jumbo < 0 || small < 0) return {};
    return { jumbo, small };
}
Time: O(1) Space: O(1)