给定一个长度为 N 的非负整数数组和一个整数 k。将给定的数组分成两个长度为 K 和 N – k 的子数组,使两个子数组之和的差最大。
例子 :
Input : arr[] = {8, 4, 5, 2, 10}
k = 2
Output : 17
Explanation :
Here, we can make first subarray of length k = {4, 2}
and second subarray of length N - k = {8, 5, 10}. Then,
the max_difference = (8 + 5 + 10) - (4 + 2) = 17.
Input : arr[] = {1, 1, 1, 1, 1, 1, 1, 1}
k = 3
Output : 2
Explanation :
Here, subarrays would be {1, 1, 1, 1, 1} and {1, 1, 1}.
So, max_difference would be 2
选择可能和最大的 k 个数字。那么解决方案显然是k个最大的数字。所以这里贪心算法起作用了——在每一步我们选择最大的可能数,直到我们得到所有 K 个数。
在这个问题中,我们应该将 N 个数字的数组分别分成k 个和N-k 个数字的两个子数组。考虑两种情况——
- 这两个子数组中和最大的子数组是K个数的子数组。然后我们想要最大化其中的总和,因为只有在第一个子阵列中的总和增加时,第二个子阵列中的总和才会减少。所以我们现在处于上面考虑的子问题中,应该选择 k 个最大的数字。
- 这两个子数组中总和最大的子数组是N-k个数的子数组。与前一种情况类似,我们必须在所有数字中选择 N – k 个最大的数字。
现在,让我们想想以上两个案例中的哪一个实际上给出了答案。我们可以很容易地看到,当更多的数字包含在最大的数字组中时,差异会更大。因此,我们可以设置 M = max(k, N – k),找到 M 个最大数的总和(假设为 S1),然后答案是 S1 – (S – S1),其中 S 是所有数字的总和。
下面是上述方法的实现:
C++
// C++ program to calculate max_difference between
// the sum of two subarrays of length k and N - k
#include
using namespace std;
// Function to calculate max_difference
int maxDifference(int arr[], int N, int k)
{
int M, S = 0, S1 = 0, max_difference = 0;
// Sum of the array
for (int i = 0; i < N; i++)
S += arr[i];
// Sort the array in descending order
sort(arr, arr + N, greater());
M = max(k, N - k);
for (int i = 0; i < M; i++)
S1 += arr[i];
// Calculating max_difference
max_difference = S1 - (S - S1);
return max_difference;
}
// Driver function
int main()
{
int arr[] = { 8, 4, 5, 2, 10 };
int N = sizeof(arr) / sizeof(arr[0]);
int k = 2;
cout << maxDifference(arr, N, k) << endl;
return 0;
}
Java
// Java program to calculate max_difference between
// the sum of two subarrays of length k and N - k
import java.util.*;
class GFG
{
// Function to calculate max_difference
static int maxDifference(int arr[], int N, int k)
{
int M, S = 0, S1 = 0, max_difference = 0;
// Sum of the array
for (int i = 0; i < N; i++)
S += arr[i];
int temp;
// Sort the array in descending order
for (int i = 0; i < N; i++)
{
for (int j = i + 1; j < N; j++)
{
if (arr[i] < arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
M = Math.max(k, N - k);
for (int i = 0; i < M; i++)
S1 += arr[i];
// Calculating max_difference
max_difference = S1 - (S - S1);
return max_difference;
}
// Driver Code
public static void main(String args[])
{
int arr[] = { 8, 4, 5, 2, 10 };
int N = arr.length;
int k = 2;
System.out.println(maxDifference(arr, N, k));
}
}
// This code is contributed by
// Surendra_Gangwar
Python3
# Python3 code to calculate max_difference
# between the sum of two subarrays of
# length k and N - k
# Function to calculate max_difference
def maxDifference(arr, N, k ):
S = 0
S1 = 0
max_difference = 0
# Sum of the array
for i in range(N):
S += arr[i]
# Sort the array in descending order
arr.sort(reverse=True)
M = max(k, N - k)
for i in range( M):
S1 += arr[i]
# Calculating max_difference
max_difference = S1 - (S - S1)
return max_difference
# Driver Code
arr = [ 8, 4, 5, 2, 10 ]
N = len(arr)
k = 2
print(maxDifference(arr, N, k))
# This code is contributed by "Sharad_Bhardwaj".
C#
// C# program to calculate max_difference between
// the sum of two subarrays of length k and N - k
using System;
class GFG
{
// Function to calculate max_difference
static int maxDifference(int []arr, int N, int k)
{
int M, S = 0, S1 = 0, max_difference = 0;
// Sum of the array
for (int i = 0; i < N; i++)
S += arr[i];
int temp;
// Sort the array in descending order
for (int i = 0; i < N; i++)
{
for (int j = i + 1; j < N; j++)
{
if (arr[i] < arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
M = Math.Max(k, N - k);
for (int i = 0; i < M; i++)
S1 += arr[i];
// Calculating max_difference
max_difference = S1 - (S - S1);
return max_difference;
}
// Driver Code
public static void Main()
{
int []arr = { 8, 4, 5, 2, 10 };
int N = arr.Length;
int k = 2;
Console.Write(maxDifference(arr, N, k));
}
}
// This code is contributed by mohit kumar 29
PHP
Javascript
输出 :
17
进一步优化:我们可以使用堆(或优先级队列)高效地找到 M 个最大的元素。有关详细信息,请参阅数组中的 k 个最大(或最小)元素。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。