Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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 
 

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

 



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




// C++ implementation of the approach
#include <bits/stdc++.h>
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




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




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




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

Javascript




<script>
// javascript implementation of the above approach
 
 
// Function to that returns true if
// it possible to choose the elements
function isPossible(A , B , n, m , x , y)
{
 
    // If elements can't be chosen
    if (x > n || 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
 
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");
 
 
// This code is contributed by 29AjayKumar
 
</script>
Output: 
Yes

 

Time Complexity: O(N*log(N))

Auxiliary Space: O(1)




My Personal Notes arrow_drop_up
Recommended Articles
Page :