Maximize 3rd element sum in quadruplet sets formed from given Array
Given an array arr containing N values describing the priority of N jobs. The task is to form sets of quadruplets (W, X, Y, Z) to be done each day such that W >= X >= Y >= Z and in doing so, maximize the sum of all Y across all quadruplet sets.
Note: N will always be a multiple of 4.
Examples:
Input: Arr[] = {2, 1, 7, 5, 5, 4, 1, 1, 3, 3, 2, 2}
Output: 10
Explanation:
We can form 3 quadruplet sets as [7, 5, 5, 1], [4, 3, 3, 1], [2, 2, 2, 1].
The summation of all Y’s are 5 + 3 + 2 = 10 which is the maximum possible value.
Input: arr[] = {1, 51, 91, 1, 1, 16, 1, 51, 48, 16, 1, 49}
Output: 68
Approach: To solve the problem mentioned above we can observe that:
- Irrespective of Y, (W, X) >= Y, i.e., higher values of W and X are always lost and don’t contribute to the answer. Therefore, we must keep these values as low as possible but greater or equal to Y.
- Similarly, value for Z is always lost and must be less than Y. Therefore, it must be as low as possible.
Hence, to satisfy the above condition we have to:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int formQuadruplets( int arr[], int n)
{
int ans = 0, pairs = 0;
pairs = n / 4;
sort(arr, arr + n, greater< int >());
for ( int i = 0; i < n - pairs; i += 3) {
ans += arr[i + 2];
}
return ans;
}
int main()
{
int arr[] = { 2, 1, 7, 5, 5,
4, 1, 1, 3, 3,
2, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << formQuadruplets(arr, n)
<< endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int formQuadruplets(Integer arr[], int n)
{
int ans = 0 , pairs = 0 ;
pairs = n / 4 ;
Arrays.sort(arr, Collections.reverseOrder());
for ( int i = 0 ; i < n - pairs; i += 3 )
{
ans += arr[i + 2 ];
}
return ans;
}
public static void main(String[] args)
{
Integer arr[] = { 2 , 1 , 7 , 5 , 5 , 4 ,
1 , 1 , 3 , 3 , 2 , 2 };
int n = arr.length;
System.out.print(
formQuadruplets(arr, n) + "\n" );
}
}
|
Python3
def formQuadruplets(arr, n):
ans = 0
pairs = 0
pairs = n / / 4
arr.sort(reverse = True )
for i in range ( 0 , n - pairs, 3 ):
ans + = arr[i + 2 ]
return ans
arr = [ 2 , 1 , 7 , 5 , 5 , 4 , 1 , 1 , 3 , 3 , 2 , 2 ]
n = len (arr)
print (formQuadruplets(arr, n))
|
C#
using System;
class GFG{
static int formQuadruplets( int []arr, int n)
{
int ans = 0, pairs = 0;
pairs = n / 4;
Array.Sort(arr);
Array.Reverse(arr);
for ( int i = 0; i < n - pairs; i += 3)
{
ans += arr[i + 2];
}
return ans;
}
public static void Main(String[] args)
{
int []arr = { 2, 1, 7, 5, 5, 4,
1, 1, 3, 3, 2, 2 };
int n = arr.Length;
Console.Write(formQuadruplets(arr, n) + "\n" );
}
}
|
Javascript
<script>
function formQuadruplets(arr, n) {
var ans = 0,
pairs = 0;
pairs = parseInt(n / 4);
arr.sort().reverse();
for ( var i = 0; i < n - pairs; i += 3) {
ans += arr[i + 2];
}
return ans;
}
var arr = [2, 1, 7, 5, 5, 4, 1, 1, 3, 3, 2, 2];
var n = arr.length;
document.write(formQuadruplets(arr, n) + "<br>" );
</script>
|
Time Complexity: O(n*log(n))
Auxiliary Space: O(1)
Last Updated :
28 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...