Given two arrays **arr1[] **and** arr2[] **consisting of **N** and **M** integers respectively, and two integers **X** and **Y**, the task is to check if it is possible to choose** X **elements from **arr1[]** and** Y** elements from **arr2[]** such that the largest among these **X **elements is less than the minimum element among these **Y **elements. If it is possible, then print **“Yes”**. Otherwise, print **“No”**.

**Examples:**

Input:arr1[] = {1, 1, 1, 1, 1}, arr2[] = {2, 2}, X = 3, Y = 1Output:YesExplanation:Every possible selection satisfies the above condition as every element of arr1[] is less than minimum element in the arr2[].

Input:arr1[] = {1, 2, 3}, arr2[] = {3, 4, 5}, X = 2, Y = 1Output:YesExplanation:One possible selection is take elements at indices 0 and 1 from arr1[] and indices 0 from arr2[], i.e {1, 2} and {3}.

**Approach:** The idea is to sort both the arrays in ascending order and then, choose the first **X** elements from **arr1[] **and the last** Y **elements from** arr2[]**. Follow the steps below to solve the problem:

- Sort both the arrays in ascending order.
- If
**X**is greater than**N**or**Y**is greater than**M**, then print**“No”**as it is not possible to choose any such combinations. - Otherwise, if the value of
**arr1[X – 1]**is less than**arr2[M – Y]**, then print**“Yes”**. - Otherwise, print
**“No”**. If none of the above conditions are satisfied.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to check if it is possible to` `// choose X and Y elements from a[] and` `// b[] such that maximum element among` `// X element is less than minimum` `// element among Y elements` `string check(` `int` `a[], ` `int` `b[], ` `int` `Na,` ` ` `int` `Nb, ` `int` `k, ` `int` `m)` `{` ` ` `// Check if there are atleast X` ` ` `// elements in arr1[] and atleast` ` ` `// Y elements in arr2[]` ` ` `if` `(Na < k || Nb < m)` ` ` `return` `"No"` `;` ` ` `// Sort arrays in ascending order` ` ` `sort(a, a + Na);` ` ` `sort(b, b + Nb);` ` ` `// Check if (X - 1)-th element in arr1[]` ` ` `// is less than from M-Yth element` ` ` `// in arr2[]` ` ` `if` `(a[k - 1] < b[Nb - m]) {` ` ` `return` `"Yes"` `;` ` ` `}` ` ` `// Return false` ` ` `return` `"No"` `;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `arr1[] = { 1, 2, 3 };` ` ` `int` `arr2[] = { 3, 4, 5 };` ` ` `int` `N = ` `sizeof` `(arr1) / ` `sizeof` `(arr1[0]);` ` ` `int` `M = ` `sizeof` `(arr2) / ` `sizeof` `(arr2[0]);` ` ` `int` `X = 2, Y = 1;` ` ` `// Function Call` ` ` `cout << check(arr1, arr2, N, M, X, Y);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `import` `java.io.*;` `import` `java.util.*;` `class` `GFG{` `// Function to check if it is possible to` `// choose X and Y elements from a[] and` `// b[] such that maximum element among` `// X element is less than minimum` `// element among Y elements` `static` `String check(` `int` `[] a, ` `int` `[] b,` ` ` `int` `Na, ` `int` `Nb,` ` ` `int` `k, ` `int` `m)` `{` ` ` ` ` `// Check if there are atleast X` ` ` `// elements in arr1[] and atleast` ` ` `// Y elements in arr2[]` ` ` `if` `(Na < k || Nb < m)` ` ` `return` `"No"` `;` ` ` `// Sort arrays in ascending order` ` ` `Arrays.sort(a);` ` ` `Arrays.sort(b);` ` ` `// Check if (X - 1)-th element in arr1[]` ` ` `// is less than from M-Yth element` ` ` `// in arr2[]` ` ` `if` `(a[k - ` `1` `] < b[Nb - m])` ` ` `{` ` ` `return` `"Yes"` `;` ` ` `}` ` ` `// Return false` ` ` `return` `"No"` `;` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `[] arr1 = { ` `1` `, ` `2` `, ` `3` `};` ` ` `int` `[] arr2 = { ` `3` `, ` `4` `, ` `5` `};` ` ` `int` `N = arr1.length;` ` ` `int` `M = arr2.length;` ` ` `int` `X = ` `2` `, Y = ` `1` `;` ` ` `// Function Call` ` ` `System.out.println(check(` ` ` `arr1, arr2, N, M, X, Y));` `}` `}` `// This code is contributed by Dharanendra L V` |

## Python3

`# Python3 program for the above approach` `# Function to check if it is possible to` `# choose X and Y elements from a[] and` `# b[] such that maximum element among` `# X element is less than minimum` `# element among Y elements` `def` `check( a, b, Na, Nb, k, m):` ` ` ` ` `# Check if there are atleast X` ` ` `# elements in arr1[] and atleast` ` ` `# Y elements in arr2[]` ` ` `if` `(Na < k ` `or` `Nb < m):` ` ` `return` `"No"` ` ` `# Sort arrays in ascending order` ` ` `a.sort()` ` ` `a.sort()` ` ` `# Check if (X - 1)-th element in arr1[]` ` ` `# is less than from M-Yth element` ` ` `# in arr2[]` ` ` `if` `(a[k ` `-` `1` `] < b[Nb ` `-` `m]):` ` ` `return` `"Yes"` ` ` `# Return false` ` ` `return` `"No"` `# Driver Code` `arr1 ` `=` `[ ` `1` `, ` `2` `, ` `3` `]` `arr2 ` `=` `[ ` `3` `, ` `4` `, ` `5` `]` `N ` `=` `len` `(arr1)` `M ` `=` `len` `(arr2)` `X ` `=` `2` `Y ` `=` `1` `# Function Call` `print` `(check(arr1, arr2, N, M, X, Y))` `# This code is contributed by rohitsongh07052.` |

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG{` `// Function to check if it is possible to` `// choose X and Y elements from a[] and` `// b[] such that maximum element among` `// X element is less than minimum` `// element among Y elements` `static` `string` `check(` `int` `[] a, ` `int` `[] b,` ` ` `int` `Na, ` `int` `Nb,` ` ` `int` `k, ` `int` `m)` `{` ` ` ` ` `// Check if there are atleast X` ` ` `// elements in arr1[] and atleast` ` ` `// Y elements in arr2[]` ` ` `if` `(Na < k || Nb < m)` ` ` `return` `"No"` `;` ` ` `// Sort arrays in ascending order` ` ` `Array.Sort(a);` ` ` `Array.Sort(b);` ` ` `// Check if (X - 1)-th element in arr1[]` ` ` `// is less than from M-Yth element` ` ` `// in arr2[]` ` ` `if` `(a[k - 1] < b[Nb - m])` ` ` `{` ` ` `return` `"Yes"` `;` ` ` `}` ` ` `// Return false` ` ` `return` `"No"` `;` `}` `// Driver Code` `static` `public` `void` `Main()` `{` ` ` `int` `[] arr1 = { 1, 2, 3 };` ` ` `int` `[] arr2 = { 3, 4, 5 };` ` ` `int` `N = arr1.Length;` ` ` `int` `M = arr2.Length;` ` ` `int` `X = 2, Y = 1;` ` ` `// Function Call` ` ` `Console.WriteLine(check(` ` ` `arr1, arr2, N, M, X, Y));` `}` `}` `// This code is contributed by Dharanendra L V` |

**Output:**

Yes

**Time Complexity:** O(N*log N)**Auxiliary Space** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.