给定两个阵列的常用3 []和BRR []分别由N和K中的元素的,任务是由与阵列中的任何元素从阵列ARR []交换任何元件以找到最大子阵列从阵列ARR []综上所述可能brr []任何次数。
例子:
Input: N = 5, K = 4, arr[] = { 7, 2, -1, 4, 5 }, brr[] = { 1, 2, 3, 2 }
Output : 21
Explanation : Swapping arr[2] with brr[2] modifies arr[] to {7, 2, 3, 4, 5}
Maximum subarray sum of the array arr[] = 21
Input : N = 2, K = 2, arr[] = { -4, -4 }, brr[] = { 8, 8 }
Output : 16
Explanation: Swap arr[0] with brr[0] and arr[1] with brr[1] modifies arr[] to {8, 8}
Maximum sum subarray of the array arr[] = 16
方法:解决此问题的想法是,通过交换数组arr和brr的元素,arr中的元素也可以在三个交换中交换。以下是一些观察结果:
- 如果需要交换数组arr []中具有索引i和j的两个元素,则从数组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)