# 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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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 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

 ``
Output:
`8`

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

My Personal Notes arrow_drop_up