C++ code to find maximum fruit count to make compote


Suppose we have three numbers a, b and c. There are a lemons, b apples and c pears. To make a compote, the fruit ratio will be 1 : 2 : 4. We cannot cut any fruit into pieces. We have to find the maximum total number of lemons, apples and pears from which we can make the compote. If not possible, return 0.

So, if the input is like a = 4; b = 7; c = 13, then the output will be 21, because we can use 3 lemons, 6 apples and 12 pears, so the answer is 3 + 6 + 12 = 21.

Steps

To solve this, we will follow these steps −

return 7 * (minimum of a, floor of (b / 2) and floor of (c / 4))

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
int solve(int a, int b, int c){
   return 7 * min(a, min(b / 2, c / 4));
}
int main(){
   int a = 4;
   int b = 7;
   int c = 13;
   cout << solve(a, b, c) << endl;
}

Input

4, 7, 13

Output

21

Updated on: 15-Mar-2022

440 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements