📌  相关文章
📜  从A []中选择X个元素,从B []中选择满足给定条件的Y个元素

📅  最后修改于: 2021-05-05 02:28:41             🧑  作者: Mango

给定两个数组A []B []以及两个整数XY ,任务是从A []中选择X个元素,从B []中选择Y个元素,使得从A []中选择的所有元素都小于所有元素。从B []中选择的元素
例子:

方法:为了满足给定的条件,必须从A []中选择最少的X个元素,并且必须从B []中选择最大的Y个元素。这可以通过对两个数组进行排序,然后从A []xSmall中选择第X最小元素,以及从B []yLarge中Y最大元素来完成
这是因为如果XSMALLyLarge较小,那么所有的元素小于必定会比yLarge较小,均低于yLarge大元素比XSMALL肯定会更大。
下面是上述方法的实现:

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


输出:
Yes