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

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 <bits/stdc++.h>
using namespace std;
 
// Function to minimize answer after choosing
// all the triplets from arr[]
int minTriplets(vector<int>& 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 Code
int main()
{
    int N = 7;
 
    vector<int> arr = { 1, 2, 4, 6, 7, 8, 3 };
 
    cout << minTriplets(arr, N);
}

Java

// Java program for above approach
import 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 Code
public 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 Code
N = 7
arr = [1, 2, 4, 6, 7, 8, 3]
print(minTriplets(arr, N))
 
# This code is contributed by gfgking

C#

// C# program for above approach
using 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 Code
public 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

<script>
    // JavaScript program for above approach
 
    // Function to minimize answer after choosing
    // all the triplets from arr[]
    const minTriplets = (arr, N) => {
 
        // To store the final answer
        let ans = 0;
 
        // Sort the array
        arr.sort();
 
        // Traverse the array
        for (let 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 Code
 
    let N = 7;
 
    let arr = [1, 2, 4, 6, 7, 8, 3];
 
    document.write(minTriplets(arr, N));
 
    // This code is contributed by rakeshsahni
 
</script>

 
 

Output

10

 

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

 




Reffered: https://www.geeksforgeeks.org


Arrays

Related
Python3 Program for Segregate 0s and 1s in an array Python3 Program for Segregate 0s and 1s in an array
Python Program for Segregate 0s and 1s in an array Python Program for Segregate 0s and 1s in an array
C++ Program to Sort an array in wave form C++ Program to Sort an array in wave form
Java Program to Sort an array in wave form Java Program to Sort an array in wave form
Python Program to Sort an array in wave form Python Program to Sort an array in wave form

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
11