Check if X and Y elements can be selected from two arrays respectively such that the maximum in X is less than the minimum in Y
Last Updated :
25 Oct, 2023
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 = 1
Output: Yes
Explanation: 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 = 1
Output: Yes
Explanation: 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
JavaScript
<script>
// javascript 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
function 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 || Nb < m)
return "No";
// Sort arrays in ascending order
a.sort();
b.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
var arr1 = [ 1, 2, 3 ];
var arr2 = [ 3, 4, 5 ];
var N = arr1.length;
var M = arr2.length;
var X = 2, Y = 1;
// Function Call
document.write(check(arr1, arr2, N, M, X, Y));
// This code is contributed by todaysgaurav
</script>
Time Complexity: O(N*log N+M*log M)
Auxiliary Space O(1)
New Approach: The idea to solve this problem is to use the Binary Search algorithm. Follow the steps below to solve the problem:
- Sort both arrays in ascending order.
- For each element in arr1[], find the largest element in arr2[] that is smaller than that element using binary search.
- If the index of the largest element in arr2[] is greater than or equal to M - Y, then it is possible to choose X elements from arr1[] and Y elements from arr2[] such that the maximum element among X is less than the minimum element among Y.
- Otherwise, continue to the next element in arr1[].
- If none of the elements in arr1[] satisfy the above condition, then it is not possible to choose any such combinations.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <algorithm>
using namespace std;
// Function to check if it is possible to
// choose X and Y elements from a[] and b[]
// such that the maximum element among X element
// is less than minimum element among Y elements
bool check(int a[], int b[], int n, int m, int x, int y)
{
// Sort both arrays in ascending order
sort(a, a + n);
sort(b, b + m);
// For each element in arr1[],
// find the largest element in arr2[]
// that is smaller than that element
for (int i = 0; i < n; i++) {
int j = lower_bound(b, b + m, a[i]) - b;
// If binary search returns a negative
// number, then the element is not found
// in arr2[] and we need to convert the
// index to its corresponding positive
// value using the formula: -(index + 1)
if (j < 0) {
j = -(j + 1);
}
// If the index of the largest element
// in arr2[] is greater than or equal
// to M-Y, then it is possible to choose
// X elements from arr1[] and Y elements
// from arr2[] such that the maximum
// element among X is less than the
// minimum element among Y
if (j <= m - y && a[i + x - 1] < b[j + y - 1]) {
return true;
}
}
// If none of the elements in arr1[]
// satisfy the above condition, then
// it is not possible to choose any
// such combinations
return false;
}
// 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
if (check(arr1, arr2, n, m, x, y)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}
Java
// Java program for the above approach
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 boolean check(int[] a, int[] b, int n, int m,
int x, int y)
{
// Sort both arrays in ascending order
Arrays.sort(a);
Arrays.sort(b);
// For each element in arr1[],
// find the largest element in arr2[]
// that is smaller than that element
for (int i = 0; i < n; i++) {
int j = Arrays.binarySearch(b, a[i]);
// If binary search returns a negative
// number, then the element is not found
// in arr2[] and we need to convert the
// index to its corresponding positive
// value using the formula: -(index + 1)
if (j < 0) {
j = -(j + 1);
}
// If the index of the largest element
// in arr2[] is greater than or equal
// to M-Y, then it is possible to choose
// X elements from arr1[] and Y elements
// from arr2[] such that the maximum
// element among X is less than the
// minimum element among Y
if (j <= m - y && a[i + x - 1] < b[j + y - 1]) {
return true;
}
}
// If none of the elements in arr1[]
// satisfy the above condition, then
// it is not possible to choose any
// such combinations
return false;
}
// 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
if (check(arr1, arr2, n, m, x, y)) {
System.out.println("Yes");
}
else {
System.out.println("No");
}
}
}
Python
import bisect
def check(a, b, n, m, x, y):
# Sort both arrays in ascending order
a.sort()
b.sort()
# For each element in a[],
# find the largest element in b[]
# that is smaller than that element
for i in range(n):
j = bisect.bisect_left(b, a[i])
# If binary search returns a negative
# number, then the element is not found
# in b[] and we need to convert the
# index to its corresponding positive
# value using the formula: -(index + 1)
if j < 0:
j = -(j + 1)
# If the index of the largest element
# in b[] is greater than or equal
# to M-Y, then it is possible to choose
# X elements from a[] and Y elements
# from b[] such that the maximum
# element among X is less than the
# minimum element among Y
if j <= m - y and a[i + x - 1] < b[j + y - 1]:
return True
# If none of the elements in a[]
# satisfy the above condition, then
# it is not possible to choose any
# such combinations
return False
# Driver Code
arr1 = [1, 2, 3]
arr2 = [3, 4, 5]
n = len(arr1)
m = len(arr2)
x = 2
y = 1
# Function Call
if check(arr1, arr2, n, m, x, y):
print("Yes")
else:
print("No")
C#
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 bool check(int[] a, int[] b, int n, int m, int x,
int y)
{
// Sort both arrays in ascending order
Array.Sort(a);
Array.Sort(b);
// For each element in arr1[],
// find the largest element in arr2[]
// that is smaller than that element
for (int i = 0; i < n; i++) {
int j = Array.BinarySearch(b, a[i]);
// If binary search returns a negative
// number, then the element is not found
// in arr2[] and we need to convert the
// index to its corresponding positive
// value using the formula: -(index + 1)
if (j < 0) {
j = -(j + 1);
}
// If the index of the largest element
// in arr2[] is greater than or equal
// to M-Y, then it is possible to choose
// X elements from arr1[] and Y elements
// from arr2[] such that the maximum
// element among X is less than the
// minimum element among Y
if (j <= m - y && a[i + x - 1] < b[j + y - 1]) {
return true;
}
}
// If none of the elements in arr1[]
// satisfy the above condition, then
// it is not possible to choose any
// such combinations
return false;
}
// Driver Code
public static 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
if (check(arr1, arr2, n, m, x, y)) {
Console.WriteLine("Yes");
}
else {
Console.WriteLine("No");
}
}
}
JavaScript
// Function to check if it is possible to
// choose X and Y elements from a[] and b[]
// such that the maximum element among X element
// is less than minimum element among Y elements
function check(a, b, x, y) {
// Sort both arrays in ascending order
a.sort((x, y) => x - y);
b.sort((x, y) => x - y);
// For each element in a[],
// find the largest element in b[]
// that is smaller than that element
for (let i = 0; i < a.length; i++) {
let j = b.findIndex(el => el >= a[i]);
// If the element is not found in b[],
// findIndex returns -1, so we handle it here
if (j < 0) {
j = b.length + j;
}
// If the index of the largest element
// in b[] is less than or equal to (M - Y),
// then it is possible to choose
// X elements from a[] and Y elements
// from b[] such that the maximum
// element among X is less than the
// minimum element among Y
if (j <= b.length - y && a[i + x - 1] < b[j + y - 1]) {
return true;
}
}
// If none of the elements in a[]
// satisfy the above condition, then
// it is not possible to choose any
// such combinations
return false;
}
// Driver Code
let arr1 = [1, 2, 3];
let arr2 = [3, 4, 5];
let x = 2, y = 1;
// Function Call
if (check(arr1, arr2, x, y)) {
console.log("Yes");
} else {
console.log("No");
}
"Note: In this approach, the idea is to use the binary search algorithm to find the largest element in arr2[] that is smaller than each element in arr1[]. We then check if the index of this element is greater than or equal to M-Y and if the maximum element among X is less than the minimum element among Y."
Time Complexity:
The time complexity of the given solution is O(n log m) where n and m are the lengths of the two arrays. This is because the code involves sorting both the arrays and then performing a binary search on array b for each element of array a, which takes O(log m) time for each iteration. Therefore, the overall time complexity is O(n log m).
Auxiliary Space:
The given solution requires O(1) auxiliary space as it only uses a few variables to store the array lengths, indices, and loop variables. The space required for sorting the arrays is also O(1) as the sorting is performed in place. Therefore, the overall space complexity is O(1).
Similar Reads
Maximum elements that can be removed from front of two arrays such that their sum is at most K
Given an integer K and two arrays A[] and B[] consisting of N and M integers, the task is to maximize the number of elements that can be removed from the front of either array according to the following rules: Remove an element from the front of either array A[] and B[] such that the value of the re
15+ min read
Choose two elements from the given array such that their sum is not present in any of the arrays
Given two arrays A[] and B[], the task is to choose two elements X and Y such that X belongs to A[] and Y belongs to B[] and (X + Y) must not be present in any of the array.Examples: Input: A[] = {3, 2, 2}, B[] = {1, 5, 7, 7, 9} Output: 3 9 3 + 9 = 12 and 12 is not present in any of the given arrays
5 min read
Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
Given two unsorted arrays arr1[] and arr2[], the task is to find the sum of elements of arr1[] such that the number of elements less than or equal to them in arr2[] is maximum. Examples: Input: arr1[] = {1, 2, 3, 4, 7, 9}, arr2[] = {0, 1, 2, 1, 1, 4} Output: 20 Below table shows the count of element
15 min read
Remove minimum elements from the array such that 2*min becomes more than max
Given an unsorted array, arr[]. The task is to find the minimum number of removals required such that twice the minimum element in the array is greater than or equal to the maximum in the array.Examples: Input: arr[] = [4, 5, 100, 9, 10, 11, 12, 15, 200]Output: 4Explanation: In the given array 4 ele
7 min read
Distribute given arrays into K sets such that total sum of maximum and minimum elements of all sets is maximum
Given two arrays, the first arr[] of size N and the second brr[] of size K. The task is to divide the first array arr[] into K sets such that the i-th set should contain brr[i] elements from the second array brr[], and the total sum of maximum and minimum elements of all sets is maximum. Examples: I
8 min read
Count of pairs from arrays A and B such that element in A is greater than element in B at that index
Given two arrays A[] and B[] of size N, the task is to count the maximum number of pairs, where each pair contains one from each array, such that A[i] > B[i]. Also the array A can be rearranged any number of times. Examples: Input: A[] = {20, 30, 50}, B[]= {60, 40, 25} Output: 2 Explanation: Init
7 min read
Remove minimum elements from either side such that 2*min becomes more than max | Set 2
Given an unsorted array, trim the array such that twice of minimum is greater than the maximum in the trimmed array. Elements should be removed from either end of the array. The number of removals should be minimum. Examples: Input: arr[] = {4, 5, 100, 9, 10, 11, 12, 15, 200} Output: 4 We need to re
15+ min read
Maximum number of elements from an array B[] that are present in ranges [A[i] + K, A[i] - K]
Given two arrays A[] of size N and B[] of size M and an integer K, the task is to select at most one element from array B[] for every element A[i] such that the element lies in the range [A[i] - K, A[i] + K] ( for 0 <= i <= N - 1 ). Print the maximum number of elements that can be selected fro
7 min read
Remove minimum elements from either side such that 2*min becomes more than max
Given an unsorted array, trim the array such that twice of minimum is greater than maximum in the trimmed array. Elements should be removed either end of the array.Number of removals should be minimum. Examples: arr[] = {4, 5, 100, 9, 10, 11, 12, 15, 200} Output: 4 We need to remove 4 elements (4, 5
15+ min read
Queries to find the maximum and minimum array elements excluding elements from a given range
Given an array arr[] consisting of N integers and an array Q[][] consisting of queries of the form [L, R]., the task for each query is to find the maximum and minimum array elements in the array excluding the elements from the given range. Examples: Input: arr[] = {2, 3, 1, 8, 3, 5, 7, 4}, Q[][] = {
15+ min read