给定一个大小为 2 * N 个整数的数组。将数组分成 N 对,以使最大对和最小化。换句话说,将数组最佳划分为 N 对应该导致最大对和,它是所有可能性的其他最大对和中的最小值。
例子:
Input : N = 2
arr[] = { 5, 8, 3, 9 }
Output : (3, 9) (5, 8)
Explanation:
Possible pairs are :
1. (8, 9) (3, 5) Maximum Sum of a Pair = 17
2. (5, 9) (3, 8) Maximum Sum of a Pair = 14
3. (3, 9) (5, 8) Maximum Sum of a Pair = 13
Thus, in case 3, the maximum pair sum is minimum of all the other cases. Hence, the answer is(3, 9) (5, 8).
Input : N = 2
arr[] = { 9, 6, 5, 1 }
Output : (1, 9) (5, 6)
方法:想法是首先对给定的数组进行排序,然后遍历循环以形成对 (i, j),其中 i 将从 0 开始,j 将从数组末尾开始。递增 i 和递减 j 以形成下一对,依此类推。
下面是上述方法的实现。
C++
// CPP Program to divide the array into
// N pairs such that maximum pair is minimized
#include
using namespace std;
void findOptimalPairs(int arr[], int N)
{
sort(arr, arr + N);
// After Sorting Maintain two variables i and j
// pointing to start and end of array Such that
// smallest element of array pairs with largest
// element
for (int i = 0, j = N - 1; i <= j; i++, j--)
cout << "(" << arr[i] << ", " << arr[j] << ")" << " ";
}
// Driver Code
int main()
{
int arr[] = { 9, 6, 5, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
findOptimalPairs(arr, N);
return 0;
}
Java
// Java Program to divide the array into
// N pairs such that maximum pair is minimized
import java.io.*;
import java.util.Arrays;
class GFG {
static void findOptimalPairs(int arr[], int N)
{
Arrays.sort(arr);
// After Sorting Maintain two variables i and j
// pointing to start and end of array Such that
// smallest element of array pairs with largest
// element
for (int i = 0, j = N - 1; i <= j; i++, j--)
System.out.print( "(" + arr[i] + ", " + arr[j] + ")" + " ");
}
// Driver Code
public static void main (String[] args)
{
int arr[] = {9, 6, 5, 1};
int N = arr.length;
findOptimalPairs(arr, N);
}
}
// This code is contributed by anuj_67.
Python3
# Python 3 Program to divide the array into
# N pairs such that maximum pair is minimized
def findOptimalPairs(arr, N):
arr.sort(reverse = False)
# After Sorting Maintain two variables
# i and j pointing to start and end of
# array Such that smallest element of
# array pairs with largest element
i = 0
j = N - 1
while(i <= j):
print("(", arr[i], ",",
arr[j], ")", end = " ")
i += 1
j -= 1
# Driver Code
if __name__ == '__main__':
arr = [9, 6, 5, 1]
N = len(arr)
findOptimalPairs(arr, N)
# This code is contributed by
# Sahil_Shelangia
C#
// C# Program to divide the array into
// N pairs such that maximum pair is minimized
using System;
public class GFG{
static void findOptimalPairs(int []arr, int N)
{
Array.Sort(arr);
// After Sorting Maintain two variables i and j
// pointing to start and end of array Such that
// smallest element of array pairs with largest
// element
for (int i = 0, j = N - 1; i <= j; i++, j--)
Console.Write( "(" + arr[i] + ", " + arr[j] + ")" + " ");
}
// Driver Code
static public void Main (){
int []arr = {9, 6, 5, 1};
int N = arr.Length;
findOptimalPairs(arr, N);
// This code is contributed by ajit.
}
}
PHP
Javascript
输出:
(1, 9) (5, 6)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。