📌  相关文章
📜  检查是否可以分别从两个数组中选择 X 和 Y 元素,使得 X 中的最大值小于 Y 中的最小值

📅  最后修改于: 2021-10-26 06:57:47             🧑  作者: Mango

给定两个数组arr1[]arr2[] 分别NM 个整数组成,以及两个整数XY ,任务是检查是否可以从arr1[] 中选择X元素,从arr2[] 中选择Y元素,例如这些X元素中最大的元素小于这些Y元素中的最小元素。如果可能,则打印“是” 。否则,打印“否”

例子:

方法:我们的想法是升序两个数组进行排序,然后选择从ARR1 []ARR2的最后一个Y元素的第一个X元素[]。请按照以下步骤解决问题:

  • 按升序对两个数组进行排序。
  • 如果X大于NY大于M ,则打印“否”,因为不可能选择任何此类组合。
  • 否则,如果arr1[X – 1] 的值小于arr2[M – Y] ,则打印“Yes”
  • 否则,打印“否” 。如果以上条件都不满足。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
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


输出:
Yes

时间复杂度: O(N*log N)
辅助空间O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程