📌  相关文章
📜  最大化给定数组的所有四元组中第二个最小值的总和

📅  最后修改于: 2021-10-26 05:58:47             🧑  作者: Mango

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

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

例子:

方法:想法是使用Greedy Approach来解决这个问题。以下是步骤:

  • 初始化一个变量,比如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


Javascript


输出:
8

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程