Last Updated: 12 Apr, 2021

Burgers At Ninja-Cafe

Easy
Asked in company
Oracle

Problem statement

You are a chef at NINJA-CAFE. There are 2 types of burgers made there. 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.

You are given two integers 'TOMATO_SLICES' and 'CHEESE_SLICES' indicating the number of tomato slices and cheese slices available.

Your task is to return an array/list [JUMBO_BURGERS, SMALL_BURGERS] indicating the number of Jumbo Burgers and Small Burgers you can make such that the number of remaining 'TOMATO_SLICES' and the number of remaining 'CHEESE_SLICES' each equals to 0. If it is not possible to make the remaining 'TOMATO_SLICES' and 'CHEESE_SLICES' equal to 0 return [-1, -1].

For Example :

Given:-
‘TOMATO_SLICES’ = 6 and ‘CHEESE_SLICES’ = 2.
Therefore 1 Jumbo Burger and 1 Small Burger can be formed using the available slice completely.
Hence the answer is  [1, 1].

Input Format :

The first line of input contains an integer T denoting the number of test cases.

The next ‘T’ lines contain exactly 2 space-separated integers, ‘TOMATO_SLICES’ and ‘CHEESE_SLICES’, which represent the number of tomato slices and cheese slices available respectively.

Output Format :

For each test case, print an integer denoting the total number of burgers of each type that can be formed.

The output of each test case will be printed in a separate line.

Note :

You don't have to print anything, it has already been taken care of. Just implement the given function.

If the number of ‘CHEESE_SLICES’ or ‘TOMATO_SLICES’ can’t be made 0, then return [-1, -1]

Constraints :

1 <= T <= 10
0 <= CHEESE_SLICES,TOMATO_SLICES <= 10 ^ 8

Time limit: 1 sec

Approaches

01 Approach

The main idea is to loop from 1 to the number of ‘CHEESE_SLICES’ we have and for each, calculate if, for the given number of ‘CHEESE_SLICES’, if we can make ‘SMALL_BURGERS’, can we use the rest of the ‘TOMOATO_SLICES’ in the ‘JUMBO_BURGERS’.

  • Maintain an array ‘RES’ initialized to {-1, -1}.
  • Loop ‘SMALL_BURGER’ from 1 to ‘CHEESE_SLICES’.
    • For the current ‘SMALL_BURGER’, ‘JUMBO_BURGER’ = ‘CHEESE_SLICES’ - ‘SMALL_BURGER’.
    • If 2 * ’SMALL_BURGER’ + 4* ’JUMBO_BURGER’ equals the given ‘TOMATO_SLICES’, then we found our answer. Return {’ JUMBO_BURGER’, ’SMALL_BURGER’}.
  • Return ‘RES’ as a result.

02 Approach

The main idea is to form 2 linear equations, such that we can find the values of ‘JUMBO_BURGER’ and ‘SMALL_BURGER’ by substituting from one equation to another.

  • It is given that, jumbo burger consumes 4 tomato slices and a small burger consumes 2 tomato slices.
  • Therefore for a given ‘JUMBO_BURGER’ and ‘SMALL_BURGER’:
    • ‘JUMBO_BURGER’ * 4 + ‘SMALL_BURGER’ * 2 = ‘TOMATO_SLICES’.
  • Also, each type of burger consumes exactly 1 ‘CHEESE_SLICES’, therefore:
    • ‘JUMBO_BURGER’ + ‘SMALL_BURGER’ = ‘CHEESE_SLICES’.
  • Therefore on solving the two equations :
    • ‘JUMBO_BURGER’ = (‘TOMATO_SLICES' - 2 * ‘CHEESE_SLICES’) / 2.
    • ‘SMALL_BURGER’ = ‘CHEESE_SLICES’ - ‘JUMBO_BURGER’.
  • If any of these turn out to be negative return {-1, -1}.
  • Else return ‘JUMBO_BURGER’ and ‘SMALL_BURGER’ as result.