Dados dos arreglos prices[] , type[] y un entero S , la tarea es comprobar si se pueden seleccionar dos artículos de dos categorías diferentes sin exceder el precio total S. Cada elemento en el arreglo type[] denota la categoría del i -ésimo elemento y cada elemento de la array prices[] denota el precio del i -ésimo elemento.
Ejemplos:
Entrada: S = 10, tipo[] = {0, 1, 1, 0}, precios[] = {3, 8, 6, 5}
Salida: Sí
Explicación:
Los elementos precios[0] y precios[2] pueden ser Precios totales seleccionados
= precios[0] + precios[2] = 3 + 6 = 9Entrada: S = 15, tipo[] = {0, 1, 1, 0}, precios[] = {5, 7, 6, 5}
Salida: No
Explicación:
No hay una solución posible para que el precio total sea menor que 15.
Enfoque: la idea es iterar y elegir todos los pares posibles utilizando dos bucles anidados . Para cada par, verifique que si su categoría es diferente y su precio total es inferior a S, en caso afirmativo, entonces hay una manera de elegir dos artículos; de lo contrario, no hay artículos de pares.
A continuación se muestra la implementación del enfoque anterior:
C++14
// C++ implementation to check if // two items can be selected from // two different categories without // exceeding the total price #include <bits/stdc++.h> using namespace std; // Function to check if // two items can be selected from // two different categories without // exceeding the total price string check(int S, int prices[], int type[], int n) { // Loop to choose two different // pairs using two nested loops for (int j = 0; j < n; j++) { for (int k = j + 1; k < n; k++) { // Condition to check if the price // of these two elements is less than S if ((type[j] == 0 && type[k] == 1) || (type[j] == 1 && type[k] == 0)) { if (prices[j] + prices[k] <= S) { return "Yes"; } } } } return "No"; } int main() { int prices[] = { 3, 8, 6, 5 }; int type[] = { 0, 1, 1, 0 }; int S = 10; int n = 4; // Function Call cout << check(S, prices, type, n); return 0; }
Java
// Java implementation to check if // two items can be selected from // two different categories without // exceeding the total price import java.util.*; class GFG{ // Function to check if // two items can be selected from // two different categories without // exceeding the total price static String check(int S, int prices[], int type[], int n) { // Loop to choose two different // pairs using two nested loops for (int j = 0; j < n; j++) { for (int k = j + 1; k < n; k++) { // Condition to check if the price // of these two elements is less than S if ((type[j] == 0 && type[k] == 1) || (type[j] == 1 && type[k] == 0)) { if (prices[j] + prices[k] <= S) { return "Yes"; } } } } return "No"; } // Driver Code public static void main(String[] args) { int prices[] = { 3, 8, 6, 5 }; int type[] = { 0, 1, 1, 0 }; int S = 10; int n = 4; // Function Call System.out.print(check(S, prices, type, n)); } } // This code is contributed by sapnasingh4991
Python3
# Python3 implementation to check if # two items can be selected from # two different categories without # exceeding the total price # Function to check if # two items can be selected from # two different categories without # exceeding the total price def check(S, prices, type1, n): # Loop to choose two different # pairs using two nested loops for j in range(0, n): for k in range(j + 1, n): # Condition to check if the price # of these two elements is less than S if ((type1[j] == 0 and type1[k] == 1) or (type1[j] == 1 and type1[k] == 0)): if (prices[j] + prices[k] <= S): return "Yes"; return "No"; # Driver Code prices = [ 3, 8, 6, 5 ]; type1 = [ 0, 1, 1, 0 ]; S = 10; n = 4; # Function Call print(check(S, prices, type1, n)); # This code is contributed by Code_Mech
C#
// C# implementation to check if // two items can be selected from // two different categories without // exceeding the total price using System; class GFG{ // Function to check if two items // can be selected from two // different categories without // exceeding the total price static String check(int S, int []prices, int []type, int n) { // Loop to choose two different // pairs using two nested loops for(int j = 0; j < n; j++) { for(int k = j + 1; k < n; k++) { // Condition to check if the price // of these two elements is less than S if ((type[j] == 0 && type[k] == 1) || (type[j] == 1 && type[k] == 0)) { if (prices[j] + prices[k] <= S) { return "Yes"; } } } } return "No"; } // Driver Code public static void Main(String[] args) { int []prices = { 3, 8, 6, 5 }; int []type = { 0, 1, 1, 0 }; int S = 10; int n = 4; // Function call Console.Write(check(S, prices, type, n)); } } // This code is contributed by sapnasingh4991
Javascript
<script> // Javascript implementation to check if // two items can be selected from // two different categories without // exceeding the total price // Function to check if two items // can be selected from two // different categories without // exceeding the total price function check(S, prices, type, n) { // Loop to choose two different // pairs using two nested loops for(let j = 0; j < n; j++) { for(let k = j + 1; k < n; k++) { // Condition to check if the price // of these two elements is less than S if ((type[j] == 0 && type[k] == 1) || (type[j] == 1 && type[k] == 0)) { if (prices[j] + prices[k] <= S) { return "Yes"; } } } } return "No"; } // Driver code let prices = [ 3, 8, 6, 5 ]; let type = [ 0, 1, 1, 0 ]; let S = 10; let n = 4; // Function call document.write(check(S, prices, type, n)); // This code is contributed by mukesh07 </script>
Yes