Maximize sum of second minimums in all quadruples of a given array

• Difficulty Level : Expert
• Last Updated : 03 May, 2021

Given an array arr[] of length N, the task is to select a quadruple (i, j, k, l) and calculate the sum of the second minimums of all possible quadruples.

Note: It is guaranteed that N is a multiple of 4 and each array element can be part of a single quadruple.

Examples:

Input: arr[] = {7, 4, 5, 2, 3, 1, 5, 9}
Output: 8
Explanation:
Quadruple 1: {7, 1, 5, 9} => 2nd Minimum value = 5.
Quadruple 2: {4, 5, 2, 3} => 2nd Minimum value = 3.
Therefore, the maximum possible sum is 8.

Input: arr[] = {7, 4, 3, 3}
Output: 3

Approach: The idea is to use Greedy Approach to solve this problem. Below are the steps:

Below is the implementation of the above approach:

C++

 // C++ program for the above approach#include #include using namespace std; // Function to find maximum possible sum of// second minimums in each quadruplevoid 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 Codeint 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 quadrupledef 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 Codeif __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 approachusing 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


Output:
8

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up