# Choose X elements from A[] and Y elements from B[] which satisfy the given condition

Given two arrays A[] and B[] and two integers X and Y, the task is to choose X elements from A[] and Y elements from B[] such that all the chosen elements from A[] are less than all the chosen elements from B[]

Examples:

Input: A[] = {1, 1, 1, 1, 1}, B[] = {3, 1}, X = 3, Y = 1
Output: Yes
Choose {1, 1, 1} from A[] and {3} from B[].

Input: A[] = {5, 4}, B[] = {2, 3, 2, 2}, X = 2, Y = 1
Output: No

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

Approach: In order to satisfy the given conditions, the minimum X elements have to be chosen from A[] and the maximum Y elements have to be chosen from B[]. This can be done by sorting both the arrays and then choosing the Xth smallest element from A[] say xSmall and Yth largest element from B[] say yLarge.
This is because if xSmall is smaller than yLarge then all the elements smaller than it will definitely be smaller than yLarge and all the elements larger than yLarge will definitely be greater than xSmall.

Below is the implementation of the above approach:

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to that returns true if ` `// it possible to choose the elements ` `bool` `isPossible(``int` `A[], ``int` `B[], ``int` `n, ` `                ``int` `m, ``int` `x, ``int` `y) ` `{ ` ` `  `    ``// If elements can't be chosen ` `    ``if` `(x > n || y > m) ` `        ``return` `false``; ` ` `  `    ``// Sort both the arrays ` `    ``sort(A, A + n); ` `    ``sort(B, B + m); ` ` `  `    ``// If xth smallest element of A[] ` `    ``// is smaller than the yth ` `    ``// greatest element of B[] ` `    ``if` `(A[x - 1] < B[m - y]) ` `        ``return` `true``; ` `    ``else` `        ``return` `false``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `A[] = { 1, 1, 1, 1, 1 }; ` `    ``int` `B[] = { 2, 2 }; ` `    ``int` `n = ``sizeof``(A) / ``sizeof``(``int``); ` `    ``int` `m = ``sizeof``(B) / ``sizeof``(``int``); ` `    ``int` `x = 3, y = 1; ` ` `  `    ``if` `(isPossible(A, B, n, m, x, y)) ` `        ``cout << ``"Yes"``; ` `    ``else` `        ``cout << ``"No"``; ` ` `  `    ``return` `0; ` `} `

 `// Java implementation of the above approach  ` `import` `java.util.*; ` ` `  `class` `GFG ` `{ ` `     `  `    ``// Function to that returns true if  ` `    ``// it possible to choose the elements  ` `    ``static` `boolean` `isPossible(``int` `A[], ``int` `B[], ``int` `n,  ` `                              ``int` `m, ``int` `x, ``int` `y)  ` `    ``{  ` `     `  `        ``// If elements can't be chosen  ` `        ``if` `(x > n || y > m)  ` `            ``return` `false``;  ` `     `  `        ``// Sort both the arrays  ` `        ``Arrays.sort(A);  ` `        ``Arrays.sort(B);  ` `     `  `        ``// If xth smallest element of A[]  ` `        ``// is smaller than the yth  ` `        ``// greatest element of B[]  ` `        ``if` `(A[x - ``1``] < B[m - y])  ` `            ``return` `true``;  ` `        ``else` `            ``return` `false``;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `main (String[] args) ` `    ``{  ` `        ``int` `A[] = { ``1``, ``1``, ``1``, ``1``, ``1` `};  ` `        ``int` `B[] = { ``2``, ``2` `};  ` `        ``int` `n = A.length;  ` `        ``int` `m = B.length;;  ` `        ``int` `x = ``3``, y = ``1``;  ` `     `  `        ``if` `(isPossible(A, B, n, m, x, y))  ` `            ``System.out.println(``"Yes"``);  ` `        ``else` `            ``System.out.println(``"No"``);  ` `    ``}  ` `} ` ` `  `// This code is contributed by AnkitRai01 `

 `# Python3 implementation of the approach  ` ` `  `# Function to that returns true if  ` `# it possible to choose the elements  ` `def` `isPossible(A, B, n, m, x, y) :  ` ` `  `    ``# If elements can't be chosen  ` `    ``if` `(x > n ``or` `y > m) :  ` `        ``return` `False` ` `  `    ``# Sort both the arrays  ` `    ``A.sort()  ` `    ``B.sort()  ` ` `  `    ``# If xth smallest element of A[]  ` `    ``# is smaller than the yth  ` `    ``# greatest element of B[]  ` `    ``if` `(A[x ``-` `1``] < B[m ``-` `y]) :  ` `        ``return` `True` `    ``else` `: ` `        ``return` `False` `         `  `# Driver code  ` `A ``=` `[ ``1``, ``1``, ``1``, ``1``, ``1` `]  ` `B ``=` `[ ``2``, ``2` `]  ` `n ``=` `len``(A)  ` `m ``=` `len``(B)  ` `x ``=` `3` `y ``=` `1` ` `  `if` `(isPossible(A, B, n, m, x, y)):  ` `    ``print``(``"Yes"``)  ` `else``: ` `    ``print``(``"No"``)  ` ` `  `# This code is contributed by  ` `# divyamohan123 `

 `// C# implementation of the above approach  ` `using` `System;          ` ` `  `class` `GFG ` `{ ` `     `  `    ``// Function to that returns true if  ` `    ``// it possible to choose the elements  ` `    ``static` `bool` `isPossible(``int` `[]A, ``int` `[]B, ``int` `n,  ` `                           ``int` `m, ``int` `x, ``int` `y)  ` `    ``{  ` `     `  `        ``// If elements can't be chosen  ` `        ``if` `(x > n || y > m)  ` `            ``return` `false``;  ` `     `  `        ``// Sort both the arrays  ` `        ``Array.Sort(A);  ` `        ``Array.Sort(B);  ` `     `  `        ``// If xth smallest element of A[]  ` `        ``// is smaller than the yth  ` `        ``// greatest element of B[]  ` `        ``if` `(A[x - 1] < B[m - y])  ` `            ``return` `true``;  ` `        ``else` `            ``return` `false``;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main (String[] args) ` `    ``{  ` `        ``int` `[]A = { 1, 1, 1, 1, 1 };  ` `        ``int` `[]B = { 2, 2 };  ` `        ``int` `n = A.Length;  ` `        ``int` `m = B.Length;;  ` `        ``int` `x = 3, y = 1;  ` `     `  `        ``if` `(isPossible(A, B, n, m, x, y))  ` `            ``Console.WriteLine(``"Yes"``);  ` `        ``else` `            ``Console.WriteLine(``"No"``);  ` `    ``}  ` `} ` ` `  `// This code is contributed by Rajput-Ji `

Output:
```Yes
```

Budding Web DeveloperKeen learnerAverage CoderDancer&Social Activist

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.

Article Tags :
Practice Tags :