📌  相关文章
📜  重新排列两个给定数组以最大化相同索引元素的总和

📅  最后修改于: 2021-09-07 02:44:40             🧑  作者: Mango

给定两个大小为N 的数组A[]B[] ,任务是通过重新排列数组元素来找到 abs(A[i] – B[i]) 的最大可能总和。

例子:

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

  • 按升序对数组A[]进行排序。
  • 按降序对数组B[]进行排序。
  • 遍历两个数组并打印 abs(A[i] – B[i]) 的所有可能值的总和。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to find the maximum possible sum
// by rearranging the given array elements
int MaxRearrngeSum(int A[], int B[], int N)
{
     
    // Sort the array A[]
    // in ascending order
    sort(A, A + N);
     
     
    // Sort the array B[]
    // in descending order
    sort(B, B + N,
            greater());
     
     
    // Stores maximum possible sum
    // by rearranging array elements        
    int maxSum = 0;
     
     
    // Traverse both the arrays
    for (int i = 0; i < N; i++) {
         
         
        // Update maxSum
        maxSum += abs(A[i] - B[i]);
    }
     
    return maxSum;
 
}
 
// Driver Code
int main()
 
{
 
    int A[] = { 1, 2, 2, 4, 5 };
    int B[] = { 5, 5, 5, 6, 6 };
 
    int N = sizeof(A) / sizeof(A[0]);
 
    cout<< MaxRearrngeSum(A, B, N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.lang.Math;
import java.util.Arrays;
import java.util.Collections;
 
class GFG{
     
// Function to find the maximum possible sum
// by rearranging the given array elements
static int MaxRearrngeSum(Integer A[],
                          Integer B[],
                          int N)
{
     
    // Sort the array A[]
    // in ascending order
    Arrays.sort(A);
     
    // Sort the array B[]
    // in descending order
    Arrays.sort(B, Collections.reverseOrder());
     
    // Stores maximum possible sum
    // by rearranging array elements         
    int maxSum = 0;
     
    // Traverse both the arrays
    for(int i = 0; i < N; i++)
    {
        // Update maxSum
        maxSum += Math.abs(A[i] - B[i]);
    }
    return maxSum;
}
   
// Driver code
public static void main (String[] args)
{
    Integer A[] = { 1, 2, 2, 4, 5 };
    Integer B[] = { 5, 5, 5, 6, 6 };
 
    int N = A.length;
   
    System.out.println(MaxRearrngeSum(A, B, N));
}
}
 
// This code is contributed by ujjwalgoel1103


Python3
# Python3 program to implement
# the above approach
 
# Function to find the maximum possible sum
# by rearranging the given array elements
def MaxRearrngeSum(A, B, N):
     
    # Sort the array A[]
    # in ascending order
    A.sort()
 
    # Sort the array B[]
    # in descending order
    B.sort(reverse = True)
 
    # Stores maximum possible sum
    # by rearranging array elements
    maxSum = 0
 
    # Traverse both the arrays
    for i in range(N):
 
        # Update maxSum
        maxSum += abs(A[i] - B[i])
 
    return maxSum
 
# Driver Code
if __name__ == "__main__":
 
    A = [ 1, 2, 2, 4, 5 ]
    B = [ 5, 5, 5, 6, 6 ]
 
    N = len(A)
 
    print(MaxRearrngeSum(A, B, N))
 
# This code is contributed by chitranayal


C#
// Java program to implement
// the above approach
using System;
 
class GFG{
     
// Function to find the maximum possible sum
// by rearranging the given array elements
static int MaxRearrngeSum(int []A, int []B, int N)
{
     
    // Sort the array A[]
    // in ascending order
    Array.Sort(A);
     
    // Sort the array B[]
    // in descending order
    Array.Sort(B);
    Array.Reverse(B);
     
    // Stores maximum possible sum
    // by rearranging array elements         
    int maxSum = 0;
     
    // Traverse both the arrays
    for(int i = 0; i < N; i++)
    {
         
        // Update maxSum
        maxSum += Math.Abs(A[i] - B[i]);
    }
    return maxSum;
}
   
// Driver code
public static void Main()
{
    int []A = { 1, 2, 2, 4, 5 };
    int []B = { 5, 5, 5, 6, 6 };
 
    int N = A.Length;
   
    Console.WriteLine(MaxRearrngeSum(A, B, N));
}
}
 
// This code is contributed by ipg2016107


Javascript


输出:
13

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live