📌  相关文章
📜  检查一个数组是否可以放入另一个数组中,以重新排列该数组中的元素

📅  最后修改于: 2021-05-04 07:36:21             🧑  作者: Mango

给定两个具有相同大小N的数组A和B。检查数组A是否可以适合于数组B。如果通过排列两个数组的元素都存在一个解决方案,使得第i元素适合,则称该数组适合另一个数组第一个数组的第一个元素的个数小于或等于第二个数组的i元素。

例子

Input : A[] = { 7, 5, 3, 2 }, B[] = { 5, 4, 8, 7 }
Output : YES 
Rearrange the first array to {3, 2, 7, 5} 
Do not rearrange the second array's element. 
After rearranging, all Ai<=Bi. 

Input : A[] = { 7, 5, 3, 2, 5, 105, 45, 10 }, B[] = { 2, 4, 0, 5, 6, 9, 75, 84 }
Output : NO

方法:对两个数组都进行排序,并检查所有0≤i≤N的A i是否小于或等于B i 。如果在i位置A i大于B i ,则返回false,否则返回true。
下面是上述方法的实现:

C++
// C++ Program to check whether an array
// can be fit into another array with given
// condition.
#include 
using namespace std;
  
// Returns true if the array A can be fit into
// array B, otherwise false
bool checkFittingArrays(int A[], int B[], int N)
{
    // Sort both the arrays
    sort(A, A + N);
    sort(B, B + N);
  
    // Iterate over the loop and check whether every
    // array element of A is less than or equal to
    // its corresponding array element of B
    for (int i = 0; i < N; i++)
        if (A[i] > B[i])
            return false;
  
    return true;
}
  
// Driver Code
int main()
{
    int A[] = { 7, 5, 3, 2 };
    int B[] = { 5, 4, 8, 7 };
    int N = sizeof(A) / sizeof(A[0]);
  
    if (checkFittingArrays(A, B, N))
        cout << "YES";
    else
        cout << "NO";
    return 0;
}


Java
// Java Program to check
// whether an array can
// be fit into another
// array with given
// condition.
import java.io.*;
import java.util.*;
import java.lang.*;
  
class GFG 
{
  
// Returns true if the
// array A can be fit 
// into array B, 
// otherwise false
static boolean checkFittingArrays(int []A,
                                  int []B, 
                                  int N)
{
      
    // Sort both the arrays
    Arrays.sort(A);
    Arrays.sort(B);
  
    // Iterate over the loop
    // and check whether every
    // array element of A is 
    // less than or equal to
    // its corresponding array 
    // element of B
    for (int i = 0; i < N; i++)
        if (A[i] > B[i])
            return false;
  
    return true;
}
  
// Driver Code
public static void main(String[] args)
{
    int A[] = {7, 5, 3, 2};
    int B[] = {5, 4, 8, 7};
    int N = A.length;
      
    if (checkFittingArrays(A, B, N))
        System.out.print("YES");
    else
        System.out.print("NO");
}
}


Python3
# Python3 Program to check whether an array
# can be fit into another array with given
# condition.
  
# Returns true if the array A can be fit into
# array B, otherwise false
def checkFittingArrays(A, B, N):
      
    # Sort both the arrays
    A = sorted(A)
    B = sorted(B)
  
    # Iterate over the loop and check whether 
    # every array element of A is less than 
    # or equal to its corresponding array 
    # element of B
    for i in range(N):
        if (A[i] > B[i]):
            return False
  
    return True
  
# Driver Code
A = [7, 5, 3, 2]
B = [5, 4, 8, 7]
N = len(A)
  
if (checkFittingArrays(A, B, N)):
    print("YES")
else:
    print("NO")
  
# This code is contributed 
# by mohit kumar


C#
// C# Program to check
// whether an array can
// be fit into another
// array with given
// condition.
using System;
  
class GFG 
{
  
// Returns true if the
// array A can be fit 
// into array B, 
// otherwise false
static bool checkFittingArrays(int []A,
                               int []B, 
                               int N)
{
      
    // Sort both the arrays
    Array.Sort(A);
    Array.Sort(B);
  
    // Iterate over the loop
    // and check whether every
    // array element of A is 
    // less than or equal to
    // its corresponding array 
    // element of B
    for (int i = 0; i < N; i++)
        if (A[i] > B[i])
            return false;
  
    return true;
}
  
// Driver Code
public static void Main () 
{
    int []A = {7, 5, 3, 2};
    int []B = {5, 4, 8, 7};
    int N = A.Length;
      
    if (checkFittingArrays(A, B, N))
        Console.WriteLine("YES");
    else
        Console.WriteLine("NO");
}
}
  
// This code is contributed 
// by anuj_67.


PHP
 $B[$i])
            return false;
  
    return true;
}
  
// Driver Code
$A = array( 7, 5, 3, 2 );
$B = array( 5, 4, 8, 7 );
$N = count($A);
  
if (checkFittingArrays($A, $B, $N))
    echo "YES";
else
    echo "NO";
      
// This code is contributed by shs
?>


输出:
YES

时间复杂度:O(N * logN),其中N是数组的大小。