给定两个数组A []和B []以及两个整数X和Y ,任务是从A []中选择X个元素,从B []中选择Y个元素,使得从A []中选择的所有元素都小于所有元素。从B []中选择的元素
例子:
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
方法:为了满足给定的条件,必须从A []中选择最少的X个元素,并且必须从B []中选择最大的Y个元素。这可以通过对两个数组进行排序,然后从A []说xSmall中选择第X个最小元素,以及从B []说yLarge中第Y个最大元素来完成。
这是因为如果XSMALL比yLarge较小,那么所有的元素小于必定会比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