Maximum items that can be bought with the given type of coins | Javascript 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
const COST = 3 ; function maxItems ( x , y , z ) { let type1 = parseInt ( x / COST ) ; x %= COST ; let type2 = parseInt ( y / COST ) ; y %= COST ; let type3 = parseInt ( z / COST ) ; z %= COST ; let type4 = Math . min ( x , Math . min ( y , z ) ) ; let maxItems = type1 + type2 + type3 + type4 ; return maxItems ; } let x = 4 , y = 5 , z = 6 ; document . write ( maxItems ( x , y , z ) ) ;