📌  相关文章
📜  检查映射是否可以使第一个数组的总和大于第二个数组

📅  最后修改于: 2021-10-25 10:05:51             🧑  作者: Mango

给定一个正整数K和两个大小分别为NM 的数组A[]B[] ,每个数组都包含[1, K]范围内的元素。任务是使用函数f将所有数字从1映射到K ,使得f(1) ,并找出f(A[ i])对于0≤i大于f(B[i])对于0≤i的总和。

例子:

处理方法:按照以下步骤解决问题:

  • 如果N>M的值,则打印“YES”作为答案,因为总是可以进行分配以使A的总和大于B
  • 否则,按降序对数组A[]B[]进行排序。
  • [0 , N-1]范围内迭代,使用变量i ,如果有任何索引, A[i]>B[i] ,打印“YES”作为答案,因为为A[i]分配了足够大的值将使A[]的总和大于B[]
  • 否则,打印“NO”作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if mapping is
// possible to make sum of first array
// larger than second array
int isPossible(int A[], int B[], int K, int N, int M)
{
    // If N > M, an assignment is
    // always possible
    if (N > M)
        return 1;
 
    // Sort A[] in descending order
    sort(A, A + N, greater());
 
    // Sort B[] in descending order
    sort(B, B + M, greater());
 
    // Traverse in the arrays
    for (int i = 0; i < N; i++)
 
        // If A[i] > B[i], assignment
        // is possible
        if (A[i] > B[i])
            return 1;
   
    return 0;
}
 
// Driver Code
int main()
{
    // Given Input
    int A[] = { 2, 2, 2 };
    int B[] = { 1, 1, 3 };
    int K = 3;
    int N = sizeof(A) / sizeof(A[0]);
    int M = sizeof(B) / sizeof(B[0]);
 
    // Function Call
    if (isPossible(A, B, K, N, M))
        cout << "YES";
    else
        cout << "NO";
 
    return 0;
}


Java
// Java Program for the above approach
import java.io.*;
import java.util.Arrays;
import java.util.Collections;
 
class GFG {
    public static void reverse(int array[])
    {
 
        // Length of the array
        int n = array.length;
 
        // Swaping the first half elements with last half
        // elements
        for (int i = 0; i < n / 2; i++) {
 
            // Storing the first half elements temporarily
            int temp = array[i];
 
            // Assigning the first half to the last half
            array[i] = array[n - i - 1];
 
            // Assigning the last half to the first half
            array[n - i - 1] = temp;
        }
    }
    // Function to check if mapping is
    // possible to make sum of first array
    // larger than second array
    public static int isPossible(int A[], int B[], int K,
                                 int N, int M)
    {
        // If N > M, an assignment is
        // always possible
        if (N > M)
            return 1;
 
        // Sort A[] in descending order
        Arrays.sort(A);
        reverse(A);
 
        // Sort B[] in descending order
        Arrays.sort(B);
        reverse(B);
 
        // Traverse in the arrays
        for (int i = 0; i < N; i++)
 
            // If A[i] > B[i], assignment
            // is possible
            if (A[i] > B[i])
                return 1;
 
        return 0;
    }
 
    public static void main(String[] args)
    {
        int A[] = { 2, 2, 2 };
        int B[] = { 1, 1, 3 };
        int K = 3;
        int N = A.length;
        int M = B.length;
 
        // Function Call
        if (isPossible(A, B, K, N, M) == 1)
            System.out.println("YES");
        else
            System.out.println("NO");
    }
}
 
// This code is contributed by lokeshpotta20


Python3
# Python3 program for the above approach
 
# Function to check if mapping is
# possible to make sum of first array
# larger than second array
def isPossible(A, B, K, N, M):
     
    # If N > M, an assignment is
    # always possible
    if (N > M):
        return 1
 
    # Sort A[] in descending order
    A.sort(reverse = True)
 
    # Sort B[] in descending order
    B.sort(reverse = True)
 
    # Traverse in the arrays
    for i in range(N):
 
        # If A[i] > B[i], assignment
        # is possible
        if (A[i] > B[i]):
            return 1
   
    return 0
 
# Driver Code
if __name__ == '__main__':
     
    # Given Input
    A = [ 2, 2, 2 ]
    B = [ 1, 1, 3 ]
    K = 3
    N = len(A)
    M = len(B)
 
    # Function Call
    if (isPossible(A, B, K, N, M)):
        print("YES")
    else:
        print("NO")
     
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
  
// Function to check if mapping is
// possible to make sum of first array
// larger than second array
static int isPossible(int []A, int []B, int K,
                      int N, int M)
{
     
    // If N > M, an assignment is
    // always possible
    if (N > M)
        return 1;
 
    // Sort A[] in descending order
    Array.Sort(A);
    Array.Reverse(A);
 
    // Sort B[] in descending order
    Array.Sort(B);
    Array.Reverse(B);
 
    // Traverse in the arrays
    for(int i = 0; i < N; i++)
     
        // If A[i] > B[i], assignment
        // is possible
        if (A[i] > B[i])
            return 1;
   
    return 0;
}
 
// Driver Code
public static void Main()
{
     
    // Given Input
    int []A = { 2, 2, 2 };
    int []B = { 1, 1, 3 };
    int K = 3;
    int N = A.Length;
    int M = B.Length;
 
    // Function Call
    if (isPossible(A, B, K, N, M) == 1)
        Console.Write("YES");
    else
        Console.Write("NO");
}
}
 
// This code is contributed by ipg2016107


Javascript


输出
YES

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