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



In this article, we will explain maximum fruit count to make compote problem and implement a C++ program to solve it. The problem involves finding the maximum number of fruits that can be used to make a compote, given certain constraints. Let's break down the problem statement below.

Maximum Fruit Count to Make Compote

You are given a list of fruits, say a apples, b bananas, and c cherries, where a, b, and c are the counts of each type of fruit. Your task is to make a compote using these fruits such that the apples, bananas and cherries are in the ratio: 1:2:4. Return the maximum number of fruits (total count) that can be used to make the compote. If it is not possible to make a compote with the given fruits, return 0.

Scenario 1

Input: a = 4, b = 7, c = 13
Output: 21

Explanation: In this case, we can use 4 apples, 6 bananas, and 12 cherries to make a compote (Considering the ratio 1:2:4). The total count of fruits used is 4 + 6 + 12 = 21 fruits, which is the maximum possible under the given constraints.

Scenario 2

Input: a = 1, b = 2, c = 3
Output: 0

Explanation: Here, we cannot make a compote with the given fruits because the counts do not satisfy the ratio 1:2:4. That is, if we take 1 apple, we would need 2 bananas and 4 cherries at least, which are not available.

Algorithm to Make Compote of Fruits

To solve this problem, we can follow these steps:

  • Input a, b and c as the counts of apples, bananas, and cherries.
  • Calculate the maximum number of compotes that can be made using the formula:
    max_compotes = min(a, b / 2, c / 4);
    
  • Calculate the total number of fruits used in the compote:
    total_fruits = max_compotes * (1 + 2 + 4);
    
  • Return total_fruits as the result.

C++ Program to Find Maximum Fruit Count

Here is the C++ code that implements the above algorithm:

#include <iostream>
#include <algorithm>
using namespace std;

int maxFruits(int a, int b, int c) {
    int sets = min({a, b / 2, c / 4});
    return sets * 7;
}

int main() {
    int a = 4, b = 7, c = 13;
    cout << maxFruits(a, b, c) << endl; // Output: 21
    return 0;
}

The output of above code will be:

21

Conclusion

In this article, we discussed the problem of finding the maximum fruit count to make a compote with given constraints. You may see this question with different fruit counts, or different ratios, but the approach is same. The key is to understand how to calculate the maximum number of sets that can be formed based on the given fruit counts and their required ratios. The C++ code provided demonstrates how to implement this logic effectively.

Farhan Muhamed
Farhan Muhamed

No Code Developer, Vibe Coder

Updated on: 2025-07-31T17:02:43+05:30

563 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements