从所有可能的三元组中最小化最小和第二最小元素的总和
给定一个数组arr[] ,任务是最小化所有可能的三元组中最小元素和第二最小元素的总和。一个元素可以恰好是一个三元组的一部分。
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
方法:这个问题可以通过使用贪心方法来解决。请按照以下步骤解决给定的问题。
- 以非递减顺序对数组arr[]进行排序,以便更容易选择每个三元组中的最小元素。
- 初始化一个变量ans = 0 ,以存储可能的最小答案。
- 遍历arr[]并通过从左侧取两个元素和从右侧取一个元素来制作每个三元组。
- 返回ans作为最终答案。
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 Code
int main()
{
int N = 7;
vector 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
输出
10
时间复杂度: O(N log N)
辅助空间: O(1)