- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# C++ code to find maximum stones we can pick from three heaps

Suppose we have three numbers a, b and c. There are three heaps of stones with a, b, and c number of stones respectively. Each time we can do these operations −

Take one stone from the first heap and two stones from the second heap (when the heaps have necessary number of stones)

Take one stone from the second heap and two stones from the third heap (when the heaps have necessary number of stones)

We have to count maximum how many stones we can collect?

So, if the input is like a = 3; b = 4; c = 5, then the output will be 9, because in two operations we can take two stones from second heap and four stones from third heap, in total we have 6 stones, then take one from first and two from second to get 3 more stones.

## Steps

To solve this, we will follow these steps −

return (minimum of b and floor of (c / 2) + minimum of a and (b - minimum of b and floor of (c / 2)) / 2) * 3

## 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 (min(b, c / 2) + min(a, (b - min(b, c / 2)) / 2)) * 3; } int main(){ int a = 3; int b = 4; int c = 5; cout << solve(a, b, c) << endl; }

## Input

3, 4, 5

## Output

9

- Related Questions & Answers
- Program to find maximum score from removing stones in Python
- C++ code to find maximum score we can assign to first student
- C++ code to find minimum stones after all operations
- C++ code to count number of times stones can be given
- Program to find number of coins we can pick from topleft to bottom-right cell and return in Python
- C++ Program to check we can remove all stones by selecting boxes
- Program to find maximum coins we can get from disappearing coins matrix in Python
- Java program to find maximum of three numbers
- Program to check we can cross river by stones or not in Python
- C++ code to find three numbers whose sum is n
- C# program to find the maximum of three numbers
- Python program to find the maximum of three numbers
- Program to find maximum amount of coin we can collect from a given matrix in Python
- C++ code to count maximum groups can be made
- C++ code to count maximum banknotes bank can gather