📌  相关文章
📜  通过串联给定两个数组的相应元素来最大化数组总和

📅  最后修改于: 2021-05-17 23:35:23             🧑  作者: Mango

给定两个相同长度的数组A []B [] ,任务是找到可以通过以任意顺序连接数组的相应元素而形成的最大数组和。

方法:想法是遍历数组以及数组的每个对应元素

  1. 通过遍历一个数字的数字将它们连接在一起,然后将数字添加到另一个数字中,该数字可以定义如下:
// Join the numbers
for digits in numberA:
    numberB = numberB*10 + digit
  1. 同样,以相反的顺序加入数字并取最大的数字。
  2. 用两者中的最大值更新结果数组的相应元素。
  3. 同样,找到结果数组的所有元素并计算总和。

下面是上述方法的实现:

C++
// C++ implementation to find the
// maximum array sum by concatenating
// corresponding elements of given two arrays
 
#include 
using namespace std;
 
// Function to join the two numbers
int joinNumbers(int numA, int numB)
{
    int revB = 0;
 
    // Loop to reverse the digits
    // of the one number
    while (numB > 0) {
        revB = revB * 10 + (numB % 10);
        numB = numB / 10;
    }
 
    // Loop to join two numbers
    while (revB > 0) {
        numA = numA * 10 + (revB % 10);
        revB = revB / 10;
    }
    return numA;
}
 
// Function to find the maximum array sum
int findMaxSum(int A[], int B[], int n)
{
    int maxArr[n];
 
    // Loop to iterate over the two
    // elements of the array
    for (int i = 0; i < n; ++i) {
 
        int X = joinNumbers(A[i], B[i]);
        int Y = joinNumbers(B[i], A[i]);
        int mx = max(X, Y);
        maxArr[i] = mx;
    }
 
    // Find the array sum
    int maxAns = 0;
    for (int i = 0; i < n; i++) {
        maxAns += maxArr[i];
    }
 
    // Return the array sum
    return maxAns;
}
 
// Driver Code
int main()
{
    int N = 5;
    int A[5] = { 11, 23, 38, 43, 59 };
    int B[5] = { 36, 24, 17, 40, 56 };
 
    cout << findMaxSum(A, B, N);
}


Java
// Java implementation to find the
// maximum array sum by concatenating
// corresponding elements of given two arrays
class GFG {
 
// Function to join the two numbers
static int joinNumbers(int numA, int numB)
{
    int revB = 0;
 
    // Loop to reverse the digits
    // of the one number
    while (numB > 0)
    {
        revB = revB * 10 + (numB % 10);
        numB = numB / 10;
    }
 
    // Loop to join two numbers
    while (revB > 0)
    {
        numA = numA * 10 + (revB % 10);
        revB = revB / 10;
    }
 
    return numA;
}
 
// Function to find the maximum array sum
static int findMaxSum(int A[], int B[], int n)
{
    int maxArr[] = new int[n];
 
    // Loop to iterate over the two
    // elements of the array
    for(int i = 0; i < n; ++i)
    {
       int X = joinNumbers(A[i], B[i]);
       int Y = joinNumbers(B[i], A[i]);
       int mx = Math.max(X, Y);
 
       maxArr[i] = mx;
    }
 
    // Find the array sum
    int maxAns = 0;
    for(int i = 0; i < n; i++)
    {
       maxAns += maxArr[i];
    }
 
    // Return the array sum
    return maxAns;
}
 
// Driver Code
public static void main(String args[])
{
    int N = 5;
    int A[] = { 11, 23, 38, 43, 59 };
    int B[] = { 36, 24, 17, 40, 56 };
 
    System.out.println(findMaxSum(A, B, N));
}
}
 
// This code is contributed by rutvik_56


Python3
# Python3 implementation to find the
# maximum array sum by concatenating
# corresponding elements of given two arrays
 
# Function to join the two numbers
def joinNumbers(numA, numB):
    revB = 0
 
    # Loop to reverse the digits
    # of the one number
    while (numB > 0):
        revB = revB * 10 + (numB % 10)
        numB = numB // 10
 
    # Loop to join two numbers
    while (revB > 0):
        numA = numA * 10 + (revB % 10)
        revB = revB // 10
 
    return numA
 
# Function to find the maximum array sum
def findMaxSum(A, B, n):
    maxArr = [0 for i in range(n)]
 
    # Loop to iterate over the two
    # elements of the array
    for i in range(n):
         
        X = joinNumbers(A[i], B[i])
        Y = joinNumbers(B[i], A[i])
        mx = max(X, Y)
        maxArr[i] = mx
 
    # Find the array sum
    maxAns = 0
     
    for i in range(n):
        maxAns += maxArr[i]
 
    # Return the array sum
    return maxAns
 
# Driver Code
if __name__ == '__main__':
     
    N = 5
    A = [ 11, 23, 38, 43, 59 ]
    B = [ 36, 24, 17, 40, 56 ]
 
    print(findMaxSum(A, B, N))
 
# This code is contributed by Samarth


C#
// C# implementation to find the
// maximum array sum by concatenating
// corresponding elements of given two arrays
using System;
class GFG{
 
// Function to join the two numbers
static int joinNumbers(int numA, int numB)
{
    int revB = 0;
 
    // Loop to reverse the digits
    // of the one number
    while (numB > 0)
    {
        revB = revB * 10 + (numB % 10);
        numB = numB / 10;
    }
 
    // Loop to join two numbers
    while (revB > 0)
    {
        numA = numA * 10 + (revB % 10);
        revB = revB / 10;
    }
 
    return numA;
}
 
// Function to find the maximum array sum
static int findMaxSum(int []A, int []B, int n)
{
    int []maxArr = new int[n];
 
    // Loop to iterate over the two
    // elements of the array
    for(int i = 0; i < n; ++i)
    {
        int X = joinNumbers(A[i], B[i]);
        int Y = joinNumbers(B[i], A[i]);
        int mx = Math.Max(X, Y);
     
        maxArr[i] = mx;
    }
 
    // Find the array sum
    int maxAns = 0;
    for(int i = 0; i < n; i++)
    {
        maxAns += maxArr[i];
    }
 
    // Return the array sum
    return maxAns;
}
 
// Driver Code
public static void Main(String []args)
{
    int N = 5;
    int []A = { 11, 23, 38, 43, 59 };
    int []B = { 36, 24, 17, 40, 56 };
 
    Console.WriteLine(findMaxSum(A, B, N));
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
19850