Minimize sum of minimum and second minimum elements from all possible triplets

• Last Updated : 03 Dec, 2021

Given an array arr[], the task is to minimize the sum of minimum and second minimum elements from all possible triplets. One element can be a part of exactly one triplet.

Input: arr[] = {1, 2, 4, 6, 7, 8, 3}, N = 7
Output: 10
Explanation: Here two triplets are formed as the size of arr[] is 7 and 7/3 = 2.
Triplet 1 – {1, 6, 3} -> Two minimum elements are 1 and 3.
Triplet 2 – {2, 4, 8} -> Two minimum elements are 2 and 4.
Hence sum = 1 + 3 + 2 + 4 = 10

Input: arr[] = {5, 7, 3, 8, 9}
Output: 8

Approach: This problem can be solved by using the Greedy Approach. Follow the steps below to solve the given problem.

• Sort the array arr[] in non-decreasing order, so that it gets easier to choose minimum elements in every triplet.
• Initialize a variable say ans = 0, to store the minimum possible answer.
• Traverse arr[] and make every triplet by taking two elements from the left side and one element from the right side.
• Return ans as the final answer.

C++14

 // C++ program for above approach#include using namespace std; // Function to minimize answer after choosing// all the triplets from arr[]int minTriplets(vector& arr, int N){     // To store the final answer    int ans = 0;     // Sort the array    sort(arr.begin(), arr.end());     // Traverse the array    for (int i = 0, j = N - 1;         i + 1 < j;         i += 2, j--) {         // Add both the smallest numbers        // of current triplet        ans += arr[i];        ans += arr[i + 1];    }     // Return the ans as the required answer    return ans;} // Driver Codeint main(){    int N = 7;     vector arr = { 1, 2, 4, 6, 7, 8, 3 };     cout << minTriplets(arr, N);}

Java

 // Java program for above approachimport java.util.*;public class GFG{   // Function to minimize answer after choosing  // all the triplets from arr[]  static int minTriplets(int []arr, int N){   // To store the final answer  int ans = 0;   // Sort the array  Arrays.sort(arr);   // Traverse the array  for (int i = 0, j = N - 1;       i + 1 < j;       i += 2, j--) {     // Add both the smallest numbers    // of current triplet    ans += arr[i];    ans += arr[i + 1];  }   // Return the ans as the required answer  return ans;} // Driver Codepublic static void main(String args[]){  int N = 7;   int []arr = { 1, 2, 4, 6, 7, 8, 3 };   System.out.print(minTriplets(arr, N));}} // This code is contributed by Samim Hossain Mondal.

Python3

 # Python program for above approach # Function to minimize answer after choosing# all the triplets from arr[]def minTriplets (arr, N) :     # To store the final answer    ans = 0     # Sort the array    arr.sort()     i = 0    j = N - 1         # Traverse the array    while( i + 1 < j):               # Add both the smallest numbers        # of current triplet        ans += arr[i]        ans += arr[i + 1]        i += 2        j -= 1             # Return the ans as the required answer    return ans # Driver CodeN = 7arr = [1, 2, 4, 6, 7, 8, 3]print(minTriplets(arr, N)) # This code is contributed by gfgking

C#

 // C# program for above approachusing System;using System.Collections;using System.Collections.Generic; class GFG{   // Function to minimize answer after choosing// all the triplets from arr[]static int minTriplets(int []arr, int N){     // To store the final answer    int ans = 0;     // Sort the array    Array.Sort(arr);     // Traverse the array    for (int i = 0, j = N - 1;         i + 1 < j;         i += 2, j--) {         // Add both the smallest numbers        // of current triplet        ans += arr[i];        ans += arr[i + 1];    }     // Return the ans as the required answer    return ans;} // Driver Codepublic static void Main(){    int N = 7;    int []arr = { 1, 2, 4, 6, 7, 8, 3 };    Console.Write(minTriplets(arr, N));}} // This code is contributed by Samim Hossain Mondal.

Javascript



Output
10

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

My Personal Notes arrow_drop_up