Given an array of size 2 * N integers. Divide the array into N pairs, such that the maximum pair sum is minimized. In other words, the optimal division of array into N pairs should result into a maximum pair sum which is minimum of other maximum pair sum of all possibilities.

**Examples:**

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)

**Approach:** The idea is to first sort the given array and then iterate over the loop to form pairs (i, j) where i would start from 0 and j would start from end of array correspondingly. Increment i and Decrement j to form the next pair and so on.

Below is the implementation of above approach.

## C++

`// CPP Program to divide the array into ` `// N pairs such that maximum pair is minimized ` `#include <bits/stdc++.h> ` ` ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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. ` ` ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP Program to divide the array into ` `// N pairs such that maximum pair is minimized ` ` ` `function` `findOptimalPairs(` `$arr` `, ` `$N` `) ` `{ ` ` ` `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` `(` `$i` `= 0, ` `$j` `= ` `$N` `- 1; ` `$i` `<= ` `$j` `; ` `$i` `++, ` `$j` `--) ` ` ` `echo` `"("` `, ` `$arr` `[` `$i` `], ` ` ` `", "` `, ` `$arr` `[` `$j` `], ` `")"` `, ` `" "` `; ` `} ` ` ` `// Driver Code ` `$arr` `= ` `array` `( 9, 6, 5, 1 ); ` `$N` `= sizeof(` `$arr` `); ` ` ` `findOptimalPairs(` `$arr` `, ` `$N` `); ` ` ` `// This code is contributed by jit_t ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

(1, 9) (5, 6)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Choose k array elements such that difference of maximum and minimum is minimized
- Choose an integer K such that maximum of the xor values of K with all Array elements is minimized
- Find a point such that sum of the Manhattan distances is minimized
- Delete odd and even numbers at alternate step such that sum of remaining elements is minimized
- Partition a set into two subsets such that difference between max of one and min of other is minimized
- Select K elements from an array whose maximum value is minimized
- Divide a sorted array in K parts with sum of difference of max and min minimized in each part
- Partition the array into two odd length groups with minimized absolute difference between their median
- Number of indices pair such that element pair sum from first Array is greater than second Array
- Probability of a random pair being the maximum weighted pair
- Find Sum of pair from two arrays with maximum sum
- Find a pair (n,r) in an integer array such that value of nPr is maximum
- Find a pair (n,r) in an integer array such that value of nCr is maximum
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Permute two arrays such that sum of every pair is greater or equal to K
- Count of distinct index pair (i, j) such that element sum of First Array is greater
- Maximum element in an array such that its previous and next element product is maximum
- Pair with given sum and maximum shortest distance from end
- Probability of choosing a random pair with maximum sum in an array
- Probability that a random pair chosen from an array (a[i], a[j]) has the maximum sum

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.