Check if two items can be selected from two different categories without exceeding price
Last Updated :
05 Jul, 2022
Given two arrays prices[], type[] and an integer S, the task is to check if two items can be selected from two different categories without exceeding the total price S. Each element in the type[] array denotes the category of the ith element and each element in the prices[] array denotes the price of the ith element.
Examples:
Input: S = 10, type[] = {0, 1, 1, 0}, prices[] = {3, 8, 6, 5}
Output: Yes
Explanation:
Elements prices[0] and prices[2] can be selected
Total prices = prices[0] + prices[2] = 3 + 6 = 9
Input: S = 15, type[] = {0, 1, 1, 0}, prices[] = {5, 7, 6, 5}
Output: No
Explanation:
There is no possible solution such that total price is less than 15.
Approach: The idea is to iterate choose every possible pair using two nested loops. For each pair check that if their category is different and their total price is less than S, If yes then there is a way to pick two items otherwise there are no such pairs items.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
string check( int S, int prices[],
int type[], int n)
{
for ( int j = 0; j < n; j++) {
for ( int k = j + 1; k < n; k++) {
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;
cout << check(S, prices, type, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static String check( int S, int prices[],
int type[], int n)
{
for ( int j = 0 ; j < n; j++)
{
for ( int k = j + 1 ; k < n; k++)
{
if ((type[j] == 0 && type[k] == 1 ) ||
(type[j] == 1 && type[k] == 0 ))
{
if (prices[j] + prices[k] <= S)
{
return "Yes" ;
}
}
}
}
return "No" ;
}
public static void main(String[] args)
{
int prices[] = { 3 , 8 , 6 , 5 };
int type[] = { 0 , 1 , 1 , 0 };
int S = 10 ;
int n = 4 ;
System.out.print(check(S, prices, type, n));
}
}
|
Python3
def check(S, prices, type1, n):
for j in range ( 0 , n):
for k in range (j + 1 , n):
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" ;
prices = [ 3 , 8 , 6 , 5 ];
type1 = [ 0 , 1 , 1 , 0 ];
S = 10 ;
n = 4 ;
print (check(S, prices, type1, n));
|
C#
using System;
class GFG{
static String check( int S, int []prices,
int []type, int n)
{
for ( int j = 0; j < n; j++)
{
for ( int k = j + 1; k < n; k++)
{
if ((type[j] == 0 && type[k] == 1) ||
(type[j] == 1 && type[k] == 0))
{
if (prices[j] + prices[k] <= S)
{
return "Yes" ;
}
}
}
}
return "No" ;
}
public static void Main(String[] args)
{
int []prices = { 3, 8, 6, 5 };
int []type = { 0, 1, 1, 0 };
int S = 10;
int n = 4;
Console.Write(check(S, prices, type, n));
}
}
|
Javascript
<script>
function check(S, prices, type, n)
{
for (let j = 0; j < n; j++)
{
for (let k = j + 1; k < n; k++)
{
if ((type[j] == 0 && type[k] == 1) ||
(type[j] == 1 && type[k] == 0))
{
if (prices[j] + prices[k] <= S)
{
return "Yes" ;
}
}
}
}
return "No" ;
}
let prices = [ 3, 8, 6, 5 ];
let type = [ 0, 1, 1, 0 ];
let S = 10;
let n = 4;
document.write(check(S, prices, type, n));
</script>
|
Share your thoughts in the comments
Please Login to comment...