Number of Burgers with No Waste of Ingredients in C++


Suppose we have two integers tomatoSlices and cheeseSlices. These are the ingredients of different burgers −

  • Jumbo Burger: 4 tomato slices and 1 cheese slice.
  • Small Burger: 2 Tomato slices and 1 cheese slice.

We have to find [total_jumbo, total_small] so that the number of tomatoSlices that are left is equal to 0 and the number of cheeseSlices that are left is also 0. If it is not possible to make the remaining tomatoSlices and cheeseSlices equal to 0 return []. So if the input is tomatoSlices = 16 and chesseSlices = 7, then the output will be [1, 6]. So this indicates, to make one jumbo burger and 6 small burgers, we need 4*1 + 2*6 = 16 tomatoSlices and 1 + 6 = 7 cheeseSlices.

To solve this, we will follow these steps −

  • make one array called ans
  • if tomato is odd or cheese > tomato/2 or tomato > 4*cheese, then return ans
  • x := (4 * cheese - tomato) / 2
  • y := (tomato – (2*x)) / 4
  • insert y then x into array ans
  • return ans

Let us see the following implementation to get better understanding −

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<auto> v){
   cout << "[";
   for(int i = 0; i<v.size(); i++){
      cout << v[i] << ", ";
   }
   cout << "]"<<endl;
}
class Solution {
   public:
   vector<int> numOfBurgers(int t, int c) {
      vector <int> ans;
      if(t % 2 != 0 || c > t/2 || t > c*4)return ans;
      int x = (4 * c - t) / 2;
      int y = ( t - (2 * x) )/ 4;
      ans.push_back(y);
      ans.push_back(x);
      return ans;
   }
};
main(){
   Solution ob;
   print_vector(ob.numOfBurgers(16,7));
}

Input

16
7

Output

[1, 6, ]

Updated on: 02-May-2020

208 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements