📌  相关文章
📜  最小化元素交换,使一个数组的总和大于其他数组

📅  最后修改于: 2022-05-13 01:56:05.513000             🧑  作者: Mango

最小化元素交换,使一个数组的总和大于其他数组

给定大小为NM的两个数组A[]B[] ,任务是找到两个数组之间的最小交换次数(可以是两个数组的任意两个元素),以使数组A[]之和严格大于数组B[]

例子:

方法:可以根据以下思路解决问题:

请按照以下步骤解决问题:

  • 按升序对数组A[]进行排序,按降序对数组B[]进行排序。
  • 计算两个数组的总和
    • 如果数组A[]的和大于数组B[]的和,则返回 0。
    • 否则,交换数组B[] 中的最大元素和数组 A[]中的最小元素,直到A []的总和不大于B[]
  • 返回使数组 A[] 严格大于数组 B[] 所需的操作数。
  • 如果不可能,则返回 -1。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find minimum operation
int minimumopretion(int A[], int N,
                    int B[], int M)
{
    sort(A, A + N);
    sort(B, B + M, greater());
 
    // Calculate sum of both array
    int suma = 0, sumb = 0;
    for (int i = 0; i < N; i++) {
        suma += A[i];
    }
    for (int i = 0; i < M; i++) {
        sumb += B[i];
    }
    int count = 0, flag = 0;
 
    // If sum of array A is strictly
    // greater than sum of array B then
    // no need to do anything
    if (suma > sumb) {
        return 0;
    }
    else {
 
        // Find min size out of
        // both array size
        int x = min(M, N);
 
        // Swapping elements more formally
        // add element in array A from B
        // and add element in array B
        // from array A
        for (int i = 0; i < x; i++) {
            suma += (B[i] - A[i]);
            sumb += (A[i] - B[i]);
 
            // Count number of operation
            count++;
 
            // If sum of array A is strictly
            // greater than array B
            // break the loop
            if (suma > sumb)
                break;
        }
 
        // Check whether it is possible
        // to make sum of array A
        // is strictly greater than array B
        if (suma <= sumb)
            return -1;
        else
            return count;
    }
}
 
// Driver Code
int main()
{
    int A[] = { 1, 5, 4, 6, 2 };
    int B[] = { 0, 1, 17, 4, 6, 2, 9 };
    int N = sizeof(A) / sizeof(A[0]);
    int M = sizeof(B) / sizeof(B[0]);
    cout << minimumopretion(A, N, B, M);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG {
 
  public static void reverse(int[] array)
  {
     
    // Length of the array
    int n = array.length;
 
    // Swapping 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 find minimum operation
  static int minimumopretion(int[] A, int N, int[] B,
                             int M)
  {
 
    Arrays.sort(A);
    Arrays.sort(B);
    reverse(B);
 
    // Calculate sum of both array
    int suma = 0, sumb = 0;
    for (int i = 0; i < N; i++) {
      suma += A[i];
    }
    for (int i = 0; i < M; i++) {
      sumb += B[i];
    }
    int count = 0, flag = 0;
 
    // If sum of array A is strictly
    // greater than sum of array B then
    // no need to do anything
    if (suma > sumb) {
      return 0;
    }
    else {
 
      // Find min size out of
      // both array size
      int x = Math.min(M, N);
 
      // Swapping elements more formally
      // add element in array A from B
      // and add element in array B
      // from array A
      for (int i = 0; i < x; i++) {
        suma += (B[i] - A[i]);
        sumb += (A[i] - B[i]);
 
        // Count number of operation
        count++;
 
        // If sum of array A is strictly
        // greater than array B
        // break the loop
        if (suma > sumb)
          break;
      }
 
      // Check whether it is possible
      // to make sum of array A
      // is strictly greater than array B
      if (suma <= sumb)
        return -1;
      else
        return count;
    }
  }
 
  // Driver Code
  public static void main (String[] args) {
    int A[] = { 1, 5, 4, 6, 2 };
    int B[] = { 0, 1, 17, 4, 6, 2, 9 };
    int N = A.length;
    int M = B.length;
    System.out.print(minimumopretion(A, N, B, M));
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# python3 program for the above approach
 
# Function to find minimum operation
from audioop import reverse
 
def minimumopretion(A, N, B, M):
    A.sort()
    B.sort(reverse = True)
 
    # Calculate sum of both array
    suma, sumb = 0, 0
    for i in range(0, N):
        suma += A[i]
 
    for i in range(0, M):
        sumb += B[i]
 
    count, flag = 0, 0
 
    # If sum of array A is strictly
    # greater than sum of array B then
    # no need to do anything
    if (suma > sumb):
        return 0
 
    else:
 
        # Find min size out of
        # both array size
        x = min(M, N)
 
        # Swapping elements more formally
        # add element in array A from B
        # and add element in array B
        # from array A
        for i in range(0, x):
            suma += (B[i] - A[i])
            sumb += (A[i] - B[i])
 
            # Count number of operation
            count += 1
 
            # If sum of array A is strictly
            # greater than array B
            # break the loop
            if (suma > sumb):
                break
 
        # Check whether it is possible
        # to make sum of array A
        # is strictly greater than array B
        if (suma <= sumb):
            return -1
        else:
            return count
 
# Driver Code
if __name__ == "__main__":
 
    A = [1, 5, 4, 6, 2]
    B = [0, 1, 17, 4, 6, 2, 9]
    N = len(A)
    M = len(B)
    print(minimumopretion(A, N, B, M))
 
# This code is contributed by rakeshsahni


C#
// C# program for the above approach
 
using System;
class GFG {
 
    // Function to find minimum operation
    static int minimumopretion(int[] A, int N, int[] B,
                               int M)
    {
        Array.Sort(A);
        Array.Sort(
            B, delegate(int m, int n) { return n - m; });
 
        // Calculate sum of both array
        int suma = 0, sumb = 0;
        for (int i = 0; i < N; i++) {
            suma += A[i];
        }
        for (int i = 0; i < M; i++) {
            sumb += B[i];
        }
        int count = 0, flag = 0;
 
        // If sum of array A is strictly
        // greater than sum of array B then
        // no need to do anything
        if (suma > sumb) {
            return 0;
        }
        else {
 
            // Find min size out of
            // both array size
            int x = Math.Min(M, N);
 
            // Swapping elements more formally
            // add element in array A from B
            // and add element in array B
            // from array A
            for (int i = 0; i < x; i++) {
                suma += (B[i] - A[i]);
                sumb += (A[i] - B[i]);
 
                // Count number of operation
                count++;
 
                // If sum of array A is strictly
                // greater than array B
                // break the loop
                if (suma > sumb)
                    break;
            }
 
            // Check whether it is possible
            // to make sum of array A
            // is strictly greater than array B
            if (suma <= sumb)
                return -1;
            else
                return count;
        }
    }
 
    // Driver Code
    public static void Main()
    {
        int[] A = { 1, 5, 4, 6, 2 };
        int[] B = { 0, 1, 17, 4, 6, 2, 9 };
        int N = A.Length;
        int M = B.Length;
        Console.Write(minimumopretion(A, N, B, M));
    }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
1

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