📌  相关文章
📜  给定阵列的所有四倍体中的第二个最小值的总和最大化

📅  最后修改于: 2021-04-18 02:36:55             🧑  作者: Mango

给定长度为N的数组arr [] ,任务是选择一个四元组(i,j,k,l)并计算所有可能的四元组的第二个最小值的和。

注意:保证N4的倍数,并且每个数组元素可以是单个四元组的一部分。

例子:

方法:想法是使用贪婪方法解决此问题。步骤如下:

  • 初始化一个变量,例如sum ,以存储来自阵列的最大增益。
  • 以升序对数组进行排序。
  • 反向遍历数组,并添加遇到的每三个元素。
  • 打印获得的总和。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
#include 
using namespace std;
 
// Function to find maximum possible sum of
// second minimums in each quadruple
void maxPossibleSum(int arr[], int N)
{
     
    // Sort the array
    sort(arr, arr + N);
 
    int sum = 0;
    int j = N - 3;
 
    while (j >= 0)
    {
         
        // Add the second minimum
        sum += arr[j];
        j -= 3;
    }
 
    // Print maximum possible sum
    cout << sum;
}
 
// Driver Code
int main()
{
     
    // Given array
    int arr[] = { 7, 4, 5, 2, 3, 1, 5, 9 };
 
    // Size of the array
    int N = 8;
 
    maxPossibleSum(arr, N);
     
    return 0;
}
 
// This code is contributed by aditya7409


Java
// Java program for the above approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to find maximum possible sum of
    // second minimums in each quadruple
    public static void maxPossibleSum(int[] arr, int N)
    {
        // Sort the array
        Arrays.sort(arr);
 
        int sum = 0;
        int j = N - 3;
 
        while (j >= 0) {
 
            // Add the second minimum
            sum += arr[j];
            j -= 3;
        }
 
        // Print maximum possible sum
        System.out.println(sum);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given array
        int[] arr = { 7, 4, 5, 2, 3, 1, 5, 9 };
 
        // Size of the array
        int N = arr.length;
 
        maxPossibleSum(arr, N);
    }
}


Python3
# Python 3 program for the above approach
 
# Function to find maximum possible sum of
# second minimums in each quadruple
def maxPossibleSum(arr,  N):
 
    # Sort the array
    arr.sort()
    sum = 0
    j = N - 3
    while (j >= 0):
 
        # Add the second minimum
        sum += arr[j]
        j -= 3
 
    # Print maximum possible sum
    print(sum)
 
# Driver Code
if __name__ == "__main__":
 
    # Given array
    arr = [7, 4, 5, 2, 3, 1, 5, 9]
 
    # Size of the array
    N = 8
    maxPossibleSum(arr, N)
 
    # This code is contributed by chitranayal


C#
// C# program for the above approach
using System;
public class GFG
{
 
  // Function to find maximum possible sum of
  // second minimums in each quadruple
  public static void maxPossibleSum(int[] arr, int N)
  {
 
    // Sort the array
    Array.Sort(arr);
    int sum = 0;
    int j = N - 3;
    while (j >= 0)
    {
 
      // Add the second minimum
      sum += arr[j];
      j -= 3;
    }
 
    // Print maximum possible sum
    Console.WriteLine(sum);
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    // Given array
    int[] arr = { 7, 4, 5, 2, 3, 1, 5, 9 };
 
    // Size of the array
    int N = arr.Length;
    maxPossibleSum(arr, N);
  }
}
 
// This code is contributed by shikhasingrajput


输出:
8

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