Dados tres números enteros X , Y y Z que representan el número de monedas para comprar algunos artículos. El costo de los artículos se da a continuación:
Tipo de artículo | Costo |
---|---|
1 | 3 monedas |
2 | 3 Y monedas |
3 | 3 monedas Z |
4 | 1 moneda X + 1 moneda Y + 1 moneda Z |
La tarea es encontrar la cantidad máxima de artículos que se pueden comprar con la cantidad dada de monedas.
Entrada: X = 4, Y = 5, Z = 6
Salida: 4
Comprar 1 artículo de tipo 1: X = 1, Y = 5, Z = 6
Comprar 1 artículo de tipo 2: X = 1, Y = 2, Z = 6
Compra 2 artículos de tipo 3: X = 1, Y = 2, Z = 0
Total de artículos comprados = 1 + 1 + 2 = 4
Entrada: X = 6, Y = 7, Z = 9
Salida: 7
Enfoque: El recuento de artículos de tipo1 , tipo2 y tipo3 que se pueden comprar será X/3 , Y/3 y Z/3 respectivamente. Ahora, la cantidad de monedas se reducirá después de comprar estos artículos como X = X % 3 , Y = Y % 3 y Z = Z % 3 . Dado que comprar el artículo del tipo 4 requiere una moneda de cada tipo. Entonces, el total de artículos del tipo 4 que se pueden comprar será el mínimo de X , Y y Z y el resultado será la suma de estos artículos que se compraron de cada tipo.
A continuación se muestra la implementación del enfoque anterior:
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; }
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
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
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..
Javascript
<script> // Javascript implementation of the approach const COST = 3; // Function to find maximum fruits // Can buy from given values of x, y, z. function maxItems(x, y, z) { // Items of type 1 that can be bought let type1 = parseInt(x / COST); // Update the coins x %= COST; // Items of type 2 that can be bought let type2 = parseInt(y / COST); // Update the coins y %= COST; // Items of type 3 that can be bought let type3 = parseInt(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 let type4 = Math.min(x, Math.min(y, z)); // Total items that can be bought let maxItems = type1 + type2 + type3 + type4; return maxItems; } // Driver code let x = 4, y = 5, z = 6; document.write(maxItems(x, y, z)); </script>
4
Complejidad de Tiempo : O(1)
Espacio Auxiliar : O(1)