📌  相关文章
📜  通过与另一个数组中的元素交换来最大化一个数组的最大可能子数组总和

📅  最后修改于: 2021-04-26 07:30:04             🧑  作者: Mango

给定两个阵列的常用3 []BRR []分别由NK中的元素的,任务是由与阵列中的任何元素从阵列ARR []交换任何元件以找到最大子阵列从阵列ARR []综上所述可能brr []任何次数。

例子:

方法:解决此问题的想法是,通过交换数组arrbrr的元素,arr中的元素也可以在三个交换中交换。以下是一些观察结果:

  • 如果需要交换数组arr []中具有索引ij的两个元素,则从数组brr []中获取任何临时元素例如在索引k处,并执行以下操作:
    • 交换arr [i]和brr [k]。
    • 交换brr [k]和arr [j]。
    • 交换arr [i]和brr [k]。
  • 现在,数组arr []brr []之间的元素也可以在数组arr []中交换。因此,将元素贪婪地排列在数组arr []中,以使其以连续方式包含所有正整数。

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

  • 将数组arr []brr []的所有元素存储在另一个数组crr []中
  • 按降序对数组crr []进行排序。
  • 计算总和,直到包含正元素的数组crr []中的最后一个索引(小于N )为止
  • 打印获得的总和。

下面是上述方法的实现。

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum subarray sum
// possible by swapping elements from array
// arr[] with that from array brr[]
void maxSum(int* arr, int* brr, int N, int K)
{
    // Stores elements from the
    // arrays arr[] and brr[]
    vector crr;
 
    // Store elements of array arr[]
    // and brr[] in the vector crr
    for (int i = 0; i < N; i++) {
        crr.push_back(arr[i]);
    }
    for (int i = 0; i < K; i++) {
        crr.push_back(brr[i]);
    }
 
    // Sort the vector crr
    // in descending order
    sort(crr.begin(), crr.end(),
         greater());
 
    // Stores maximum sum
    int sum = 0;
 
    // Calculate the sum till the last
    // index in crr[] which is less than
    // N which contains a positive element
    for (int i = 0; i < N; i++) {
        if (crr[i] > 0) {
            sum += crr[i];
        }
        else {
            break;
        }
    }
 
    // Print the sum
    cout << sum << endl;
}
 
// Driver code
int main()
{
    // Given arrays and respective lengths
    int arr[] = { 7, 2, -1, 4, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int brr[] = { 1, 2, 3, 2 };
    int K = sizeof(brr) / sizeof(brr[0]);
 
    // Calculate maximum subarray sum
    maxSum(arr, brr, N, K);
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
  // Function to find the maximum subarray sum
  // possible by swapping elements from array
  // arr[] with that from array brr[]
  static void maxSum(int arr[], int brr[], int N, int K)
  {
 
    // Stores elements from the
    // arrays arr[] and brr[]
    Vector crr = new Vector();
 
    // Store elements of array arr[]
    // and brr[] in the vector crr
    for (int i = 0; i < N; i++)
    {
      crr.add(arr[i]);
    }
    for (int i = 0; i < K; i++)
    {
      crr.add(brr[i]);
    }
 
    // Sort the vector crr
    // in descending order
    Collections.sort(crr);
    Collections.reverse(crr);
 
    // Stores maximum sum
    int sum = 0;
 
    // Calculate the sum till the last
    // index in crr[] which is less than
    // N which contains a positive element
    for (int i = 0; i < N; i++)
    {
      if (crr.get(i) > 0)
      {
        sum += crr.get(i);
      }
      else
      {
        break;
      }
    }
 
    // Print the sum
    System.out.println(sum);
  }
 
  // Driver code
  public static void main(String[] args)
  {
 
    // Given arrays and respective lengths
    int arr[] = { 7, 2, -1, 4, 5 };
    int N = arr.length;
    int brr[] = { 1, 2, 3, 2 };
    int K = brr.length;
 
    // Calculate maximum subarray sum
    maxSum(arr, brr, N, K);
  }
}
 
// This code is contributed by divyesh072019


Python3
# Python3 program for the above approach
 
# Function to find the maximum subarray sum
# possible by swapping elements from array
# arr[] with that from array brr[]
def maxSum(arr, brr, N, K):
     
    # Stores elements from the
    # arrays arr[] and brr[]
    crr = []
 
    # Store elements of array arr[]
    # and brr[] in the vector crr
    for i in range(N):
        crr.append(arr[i])
 
    for i in range(K):
        crr.append(brr[i])
 
    # Sort the vector crr
    # in descending order
    crr = sorted(crr)[::-1]
 
    # Stores maximum sum
    sum = 0
 
    # Calculate the sum till the last
    # index in crr[] which is less than
    # N which contains a positive element
    for i in range(N):
        if (crr[i] > 0):
            sum += crr[i]
        else:
            break
 
    # Print the sum
    print(sum)
 
# Driver code
if __name__ == '__main__':
     
    # Given arrays and respective lengths
    arr = [ 7, 2, -1, 4, 5 ]
    N = len(arr)
    brr = [ 1, 2, 3, 2 ]
    K = len(brr)
 
    # Calculate maximum subarray sum
    maxSum(arr, brr, N, K)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
     
// Function to find the maximum subarray sum
// possible by swapping elements from array
// arr[] with that from array brr[]
static void maxSum(int[] arr, int[] brr,
                   int N, int K)
{
     
    // Stores elements from the
    // arrays arr[] and brr[]
    List crr = new List();
  
    // Store elements of array arr[]
    // and brr[] in the vector crr
    for(int i = 0; i < N; i++)
    {
        crr.Add(arr[i]);
    }
    for(int i = 0; i < K; i++)
    {
        crr.Add(brr[i]);
    }
  
    // Sort the vector crr
    // in descending order
    crr.Sort();
    crr.Reverse();
  
    // Stores maximum sum
    int sum = 0;
  
    // Calculate the sum till the last
    // index in crr[] which is less than
    // N which contains a positive element
    for(int i = 0; i < N; i++)
    {
        if (crr[i] > 0)
        {
            sum += crr[i];
        }
        else
        {
            break;
        }
    }
  
    // Print the sum
    Console.WriteLine(sum);
}
 
// Driver Code
static void Main()
{
     
    // Given arrays and respective lengths
    int[] arr = { 7, 2, -1, 4, 5 };
    int N = arr.Length;
    int[] brr = { 1, 2, 3, 2 };
    int K = brr.Length;
     
    // Calculate maximum subarray sum
    maxSum(arr, brr, N, K);
}
}
 
// This code is contributed by divyeshrabadiya07


输出:
21

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