Problem

Given two integers tomatoSlices and cheeseSlices. The ingredients of different burgers are as follows:

  • Jumbo Burger: 4 tomato slices and 1 cheese slice.
  • Small Burger: 2 Tomato slices and 1 cheese 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

1
2
3
4
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

1
2
3
Input: tomatoSlices = 17, cheeseSlices = 4
Output: []
**Explantion:** There will be no way to use all ingredients to make small and jumbo burgers.

Example 3

1
2
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

  1. x = tomatoSlices/2 - cheeseSlices
  2. y = 2*cheeseSlices - tomatoSlices/2
  3. Both x and y must be >= 0 and integer, tomatoSlices must be even.
  4. Return [x, y] or [] if impossible.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#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};
    }
};
1
2
3
4
5
6
7
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}
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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);
    }
}
1
2
3
4
5
6
7
8
9
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)
    }
}
1
2
3
4
5
6
7
8
9
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]
1
2
3
4
5
6
7
8
9
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]
    }
}
1
2
3
4
5
6
7
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)