Check if two items can be selected from two different categories without exceeding price

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 excedding 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.

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++

 `// C++ implementation to check if ` `// two items can be selected from ` `// two different categories without ` `// exceeding the total price ` ` `  `#include ` `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 `

Output:

```Yes
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.

Improved By : sapnasingh4991

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.