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:
- Initialize a variable, say sum, to store the maximum gain from the array.
- Sort the array in ascending order.
- Traverse the array in reverse and add every third element encountered.
- Print the sum obtained.
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> |
8
Time Complexity: O(N*log(N))
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!