C++ code to find max ornaments to make decoration good


Suppose we have three numbers y, b and r. There are y yellow ornaments, b blue ornaments and r red ornaments for decoration. A decoration will be beautiful, if the number of blue ornaments used is greater by exactly 1 than the number of yellow ornaments, and the number of red ornaments used is greater by exactly 1 than the number of blue ornaments. We want to choose as many ornaments as possible and also want to make our decoration good. We have to find the maximum number of ornaments used for a beautiful decoration.

So, if the input is like y = 8; b = 13; r = 9, then the output will be 24, because 7 + 8 + 9 = 24.

Steps

To solve this, we will follow these steps −

return 3 * (minimum of y, (b - 1) and (r - 2))

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
int solve(int y, int b, int r){
   return 3 * min(y, min(b - 1, r - 2)) + 3;
}
int main(){
   int y = 8;
   int b = 13;
   int r = 9;
   cout << solve(y, b, r) << endl;
}

Input

8, 13, 9

Output

24

Updated on: 15-Mar-2022

115 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements