Choose X elements from A[] and Y elements from B[] which satisfy the given condition
Last Updated :
08 Nov, 2021
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
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++
#include <bits/stdc++.h>
using namespace std;
bool isPossible( int A[], int B[], int n,
int m, int x, int y)
{
if (x > n || y > m)
return false ;
sort(A, A + n);
sort(B, B + m);
if (A[x - 1] < B[m - y])
return true ;
else
return false ;
}
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
import java.util.*;
class GFG
{
static boolean isPossible( int A[], int B[], int n,
int m, int x, int y)
{
if (x > n || y > m)
return false ;
Arrays.sort(A);
Arrays.sort(B);
if (A[x - 1 ] < B[m - y])
return true ;
else
return false ;
}
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" );
}
}
|
Python3
def isPossible(A, B, n, m, x, y) :
if (x > n or y > m) :
return False
A.sort()
B.sort()
if (A[x - 1 ] < B[m - y]) :
return True
else :
return False
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" )
|
C#
using System;
class GFG
{
static bool isPossible( int []A, int []B, int n,
int m, int x, int y)
{
if (x > n || y > m)
return false ;
Array.Sort(A);
Array.Sort(B);
if (A[x - 1] < B[m - y])
return true ;
else
return false ;
}
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" );
}
}
|
Javascript
<script>
function isPossible(A , B , n, m , x , y)
{
if (x > n || y > m)
return false ;
A.sort();
B.sort();
if (A[x - 1] < B[m - y])
return true ;
else
return false ;
}
var A = [ 1, 1, 1, 1, 1 ];
var B = [ 2, 2 ];
var n = A.length;
var m = B.length;;
var x = 3, y = 1;
if (isPossible(A, B, n, m, x, y))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...