Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmMaximize sum of second minimums in all quadruples of a given array

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

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 <iostream>
#include <bits/stdc++.h>
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




<script>
 
// javascript program of the above approach
 
    // Function to find maximum possible sum of
    // second minimums in each quadruple
    function maxPossibleSum(arr, N)
    {
        // Sort the array
        arr.sort();
  
        let sum = 0;
        let j = N - 3;
  
        while (j >= 0) {
  
            // Add the second minimum
            sum += arr[j];
            j -= 3;
        }
  
        // Print maximum possible sum
        document.write(sum);
    }
 
    // Driver Code
     
     // Given array
        let arr = [ 7, 4, 5, 2, 3, 1, 5, 9 ];
  
        // Size of the array
        let N = arr.length;
  
        maxPossibleSum(arr, N);
 
// Thiscode is contributed by target_2.
</script>


Output: 

8

 

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

 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Last Updated :
10 Aug, 2022
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Ted Musemwa
As a software developer I’m interested in the intersection of computational thinking and design thinking when solving human problems. As a professional I am guided by the principles of experiential learning; experience, reflect, conceptualise and experiment.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments