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 [].
Input: tomatoSlices =16, cheeseSlices =7Output: [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.
Input: tomatoSlices =4, cheeseSlices =17Output: []**Explantion:** Making 1 jumbo burger there will be 16 cheese remaining and making 2 small burgers there will be 15 cheese remaining.
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.
#include<vector>usingnamespace std;
classSolution {
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};
}
};
import java.util.*;
classSolution {
public List<Integer>numOfBurgers(int tomatoSlices, int cheeseSlices) {
if (tomatoSlices%2 != 0) returnnew ArrayList<>();
int x = tomatoSlices/2 - cheeseSlices;
int y = cheeseSlices - x;
if (x < 0 || y < 0) returnnew ArrayList<>();
return Arrays.asList(x, y);
}
}
1
2
3
4
5
6
7
8
9
classSolution {
funnumOfBurgers(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
classSolution:
defnumOfBurgers(self, tomatoSlices: int, cheeseSlices: int) -> List[int]:
if tomatoSlices%2!=0:
return []
x = tomatoSlices//2- cheeseSlices
y = cheeseSlices - x
if x <0or y <0:
return []
return [x, y]
1
2
3
4
5
6
7
8
9
impl Solution {
pubfnnum_of_burgers(tomato_slices: i32, cheese_slices: i32) -> Vec<i32> {
if tomato_slices%2!=0 { returnvec![]; }
let x = tomato_slices/2- cheese_slices;
let y = cheese_slices - x;
if x <0|| y <0 { returnvec![]; }
vec![x, y]
}
}
1
2
3
4
5
6
7
functionnumOfBurgers(tomatoSlices: number, cheeseSlices: number):number[] {
if (tomatoSlices%2!==0) return [];
constx=tomatoSlices/2-cheeseSlices;
consty=cheeseSlices-x;
if (x<0||y<0) return [];
return [x, y];
}