Maximum items that can be bought with the given type of coins
Last Updated :
03 Mar, 2022
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++
#include <iostream>
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;
}
|
Java
import java.io.*;
class GFG
{
static int COST = 3 ;
static 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 = Math.min(x, Math.min(y, z));
int maxItems = type1 + type2 + type3 + type4;
return maxItems;
}
public static void main (String[] args)
{
int x = 4 , y = 5 , z = 6 ;
System.out.println(maxItems(x, y, z));
}
}
|
Python3
COST = 3 ;
def maxItems(x, y, z) :
type1 = x / / COST;
x % = COST;
type2 = y / / COST;
y % = COST;
type3 = z / / COST;
z % = COST;
type4 = min (x, min (y, z));
maxItems = type1 + type2 + type3 + type4;
return maxItems;
if __name__ = = "__main__" :
x = 4 ; y = 5 ; z = 6 ;
print (maxItems(x, y, z));
|
C#
using System;
class GFG
{
static int COST = 3;
static 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 = Math.Min(x, Math.Min(y, z));
int maxItems = type1 + type2 + type3 + type4;
return maxItems;
}
static public void Main ()
{
int x = 4, y = 5, z = 6;
Console.Write (maxItems(x, y, z));
}
}
|
Javascript
<script>
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));
</script>
|
Time Complexity : O(1)
Auxiliary Space : O(1)
Share your thoughts in the comments
Please Login to comment...