
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Find Maximum Stones We Can Pick from Three Heaps in C++
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