📌  相关文章
📜  划分为两个长度为k和(N – k)的子数组,以使总和之差最大

📅  最后修改于: 2021-05-06 09:16:50             🧑  作者: Mango

给定长度为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个最大(或最小)元素。