📌  相关文章
📜  最大化数组的每K个长度分区的第二个最小值的总和

📅  最后修改于: 2021-05-19 17:55:21             🧑  作者: Mango

给定一个大小为N的数组A []和一个正整数K (将始终是N的因数),任务是通过将该数组划分为两个数组,找到该数组每个分区的第二个最小元素的最大可能和。 (N / K)个大小相等的分区。

例子:

方法:想法是以升序对给定的数组进行排序,并且为了使所需的总和最大化,将A []的前N / K个元素除以每个数组作为它们的第一项,然后选择每个(K – 1 ) A []的N元素,从N / K开始

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

  • 对数组A []进行升序排序。
  • 0初始化以存储所需的和。
  • 现在,使用N / K初始化变量i
  • i小于N时,请执行以下步骤:
    • 总和增加A [i]
    • i递增K – 1
  • 遍历后,将总和打印为所需答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum sum of
// second smallest of each partition
// of size K
void findSum(int A[], int N, int K)
{
 
    // Sort the array A[]
    // in ascending order
    sort(A, A + N);
 
    // Store the maximum sum of second
    // smallest of each partition
    // of size K
    int sum = 0;
 
    // Select every (K-1)th element as
    // second smallest element
    for (int i = N / K; i < N; i += K - 1) {
 
        // Update sum
        sum += A[i];
    }
 
    // Print the maximum sum
    cout << sum;
}
 
// Driver Code
int main()
{
 
    // Given size of partitions
    int K = 4;
 
    // Given array A[]
    int A[] = { 2, 3, 1, 4, 7, 5, 6, 1 };
 
    // Size of the given array
    int N = sizeof(A) / sizeof(A[0]);
 
    // Function Call
    findSum(A, N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the maximum sum of
// second smallest of each partition
// of size K
static void findSum(int A[], int N, int K)
{
     
    // Sort the array A[]
    // in ascending order
    Arrays.sort(A);
     
    // Store the maximum sum of second
    // smallest of each partition
    // of size K
    int sum = 0;
 
    // Select every (K-1)th element as
    // second smallest element
    for(int i = N / K; i < N; i += K - 1)
    {
         
        // Update sum
        sum += A[i];
    }
 
    // Print the maximum sum
    System.out.print(sum);
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given size of partitions
    int K = 4;
 
    // Given array A[]
    int A[] = { 2, 3, 1, 4, 7, 5, 6, 1 };
 
    // Size of the given array
    int N = A.length;
 
    // Function Call
    findSum(A, N, K);
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program for the above approach
 
# Function to find the maximum sum of
# second smallest of each partition
# of size K
def findSum(A, N, K):
   
    # Sort the array A
    # in ascending order
    A.sort();
 
    # Store the maximum sum of second
    # smallest of each partition
    # of size K
    sum = 0;
 
    # Select every (K-1)th element as
    # second smallest element
    for i in range(N // K, N, K - 1):
       
        # Update sum
        sum += A[i];
 
    # Prthe maximum sum
    print(sum);
 
# Driver Code
if __name__ == '__main__':
   
    # Given size of partitions
    K = 4;
 
    # Given array A
    A = [2, 3, 1, 4, 7, 5, 6, 1];
 
    # Size of the given array
    N = len(A);
 
    # Function Call
    findSum(A, N, K);
 
    # This code contributed by shikhasingrajput


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the maximum sum of
// second smallest of each partition
// of size K
static void findSum(int []A, int N, int K)
{
     
    // Sort the array []A
    // in ascending order
    Array.Sort(A);
     
    // Store the maximum sum of second
    // smallest of each partition
    // of size K
    int sum = 0;
 
    // Select every (K-1)th element as
    // second smallest element
    for(int i = N / K; i < N; i += K - 1)
    {
         
        // Update sum
        sum += A[i];
    }
     
    // Print the maximum sum
    Console.Write(sum);
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given size of partitions
    int K = 4;
 
    // Given array []A
    int []A = { 2, 3, 1, 4, 7, 5, 6, 1 };
 
    // Size of the given array
    int N = A.Length;
 
    // Function Call
    findSum(A, N, K);
}
}
 
// This code is contributed by shikhasingrajput


输出:
7

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