Maximum items that can be bought with the given type of coins | C ++ implementation of the approach ; Function to find maximum fruits Can buy from given values of x , y , z . ; Items of type 1 that can be bought ; Update the coins ; Items of type 2 that can be bought ; Update the coins ; Items of type 3 that can be bought ; Update the coins ; Items of type 4 that can be bought To buy a type 4 item , a coin of each type is required ; Total items that can be bought ; Driver code
#include <iostream> NEW_LINE using namespace std ; const int COST = 3 ; int maxItems ( int x , int y , int z ) { int type1 = x / COST ; x %= COST ; int type2 = y / COST ; y %= COST ; int type3 = z / COST ; z %= COST ; int type4 = min ( x , min ( y , z ) ) ; int maxItems = type1 + type2 + type3 + type4 ; return maxItems ; } int main ( ) { int x = 4 , y = 5 , z = 6 ; cout << maxItems ( x , y , z ) ; return 0 ; }