Given three integers **X**, **Y** and **Z** which represent the number of coins to buy some items. The cost of items is given below:

Item type | Cost |
---|---|

1 | 3 X coins |

2 | 3 Y coins |

3 | 3 Z coins |

4 | 1 X coin + 1 Y coin + 1 Z coin |

The task is to find the maximum number of items that can be bought with the given number of coins.

Input:X = 4, Y = 5, Z = 6

Output:4

Buy 1 item of type 1: X = 1, Y = 5, Z = 6

Buy 1 item of type 2: X = 1, Y = 2, Z = 6

Buy 2 items of type 3: X = 1, Y = 2, Z = 0

Total items bought = 1 + 1 + 2 = 4

Input:X = 6, Y = 7, Z = 9

Output:7

**Approach:** The count of items of **type1**, **type2** and **type3** that can be bought will be **X / 3**, **Y / 3** and **Z / 3** respectively. Now, the number of coins will get reduced after buying these items as **X = X % 3**, **Y = Y % 3** and **Z = Z % 3**. Since, buying the item of type 4 requires a coin from each of the type. So, the total items of type 4 that can be bought will be the minimum of **X**, **Y** and **Z** and the result will be the sum of these items which were bought from each of the type.

Below is the implementation of the above approach:

## C++

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

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` `static` `int` `COST = ` `3` `; ` ` ` `// Function to find maximum fruits ` `// Can buy from given values of x, y, z. ` `static` `int` `maxItems(` `int` `x, ` `int` `y, ` `int` `z) ` `{ ` ` ` ` ` `// Items of type 1 that can be bought ` ` ` `int` `type1 = x / COST; ` ` ` ` ` `// Update the coins ` ` ` `x %= COST; ` ` ` ` ` `// Items of type 2 that can be bought ` ` ` `int` `type2 = y / COST; ` ` ` ` ` `// Update the coins ` ` ` `y %= COST; ` ` ` ` ` `// Items of type 3 that can be bought ` ` ` `int` `type3 = z / COST; ` ` ` ` ` `// Update the coins ` ` ` `z %= COST; ` ` ` ` ` `// Items of type 4 that can be bought ` ` ` `// To buy a type 4 item, a coin ` ` ` `// of each type is required ` ` ` `int` `type4 = Math.min(x, Math.min(y, z)); ` ` ` ` ` `// Total items that can be bought ` ` ` `int` `maxItems = type1 + type2 + type3 + type4; ` ` ` `return` `maxItems; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `x = ` `4` `, y = ` `5` `, z = ` `6` `; ` ` ` `System.out.println(maxItems(x, y, z)); ` `} ` `} ` ` ` `// This code is contributed by @tushil ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `COST ` `=` `3` `; ` ` ` `# Function to find maximum fruits ` `# Can buy from given values of x, y, z. ` `def` `maxItems(x, y, z) : ` ` ` ` ` `# Items of type 1 that can be bought ` ` ` `type1 ` `=` `x ` `/` `/` `COST; ` ` ` ` ` `# Update the coins ` ` ` `x ` `%` `=` `COST; ` ` ` ` ` `# Items of type 2 that can be bought ` ` ` `type2 ` `=` `y ` `/` `/` `COST; ` ` ` ` ` `# Update the coins ` ` ` `y ` `%` `=` `COST; ` ` ` ` ` `# Items of type 3 that can be bought ` ` ` `type3 ` `=` `z ` `/` `/` `COST; ` ` ` ` ` `# Update the coins ` ` ` `z ` `%` `=` `COST; ` ` ` ` ` `# Items of type 4 that can be bought ` ` ` `# To buy a type 4 item, a coin ` ` ` `# of each type is required ` ` ` `type4 ` `=` `min` `(x, ` `min` `(y, z)); ` ` ` ` ` `# Total items that can be bought ` ` ` `maxItems ` `=` `type1 ` `+` `type2 ` `+` `type3 ` `+` `type4; ` ` ` `return` `maxItems; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `x ` `=` `4` `; y ` `=` `5` `; z ` `=` `6` `; ` ` ` ` ` `print` `(maxItems(x, y, z)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` `static` `int` `COST = 3; ` ` ` `// Function to find maximum fruits ` `// Can buy from given values of x, y, z. ` `static` `int` `maxItems(` `int` `x, ` `int` `y, ` `int` `z) ` `{ ` ` ` ` ` `// Items of type 1 that can be bought ` ` ` `int` `type1 = x / COST; ` ` ` ` ` `// Update the coins ` ` ` `x %= COST; ` ` ` ` ` `// Items of type 2 that can be bought ` ` ` `int` `type2 = y / COST; ` ` ` ` ` `// Update the coins ` ` ` `y %= COST; ` ` ` ` ` `// Items of type 3 that can be bought ` ` ` `int` `type3 = z / COST; ` ` ` ` ` `// Update the coins ` ` ` `z %= COST; ` ` ` ` ` `// Items of type 4 that can be bought ` ` ` `// To buy a type 4 item, a coin ` ` ` `// of each type is required ` ` ` `int` `type4 = Math.Min(x, Math.Min(y, z)); ` ` ` ` ` `// Total items that can be bought ` ` ` `int` `maxItems = type1 + type2 + type3 + type4; ` ` ` `return` `maxItems; ` `} ` ` ` `// Driver code ` `static` `public` `void` `Main () ` `{ ` ` ` `int` `x = 4, y = 5, z = 6; ` ` ` ` ` `Console.Write (maxItems(x, y, z)); ` `} ` `} ` ` ` `// This code is contributed by ajit.. ` |

*chevron_right*

*filter_none*

**Output:**

4

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximum number of groups of size 3 containing two type of items
- Maximum number of candies that can be bought
- Program to find the count of coins of each type from the given ratio
- Maximum profit by selling N items at two markets
- Count of ways to distribute N items among 3 people with one person receiving maximum
- Program to find the profit or loss when CP of N items is equal to SP of M items
- Probability of distributing M items among X bags such that first bag contains N items
- Maximum length of subarray consisting of same type of element on both halves of sub-array
- Check if given coins can be used to pay a value of S
- Probability of getting at least K heads in N tosses of Coins
- Find minimum number of coins that make a given value
- Minimum number of coins that can generate all the values in the given range
- Find out the minimum number of coins required to pay total amount
- Probability of getting two consecutive heads after choosing a random coin among two different types of coins
- Find the top K items with the highest value
- Minimum number of items to be delivered
- Ways to place 4 items in n^2 positions such that no row/column contains more than one
- Count ways to distribute m items among n people
- Distributing M items in a circle of size N starting from K-th position
- Calculate the loss incurred in selling the given items at discounted price

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.