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

📅  最后修改于: 2021-09-03 13:40:39             🧑  作者: Mango

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

例子:

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

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

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

下面是上述方法的实现:

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];
 
    # Print the 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


Javascript


输出:
7

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live