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 :
- (8, 9) (3, 5) Maximum Sum of a Pair = 17
- (5, 9) (3, 8) Maximum Sum of a Pair = 14
- (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++
#include <bits/stdc++.h>
using namespace std;
void findOptimalPairs( int arr[], int N)
{
sort(arr, arr + N);
for ( int i = 0, j = N - 1; i <= j; i++, j--)
cout << "(" << arr[i] << ", " << arr[j] << ")" << " " ;
}
int main()
{
int arr[] = { 9, 6, 5, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
findOptimalPairs(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;
class GFG {
static void findOptimalPairs( int arr[], int N)
{
Arrays.sort(arr);
for ( int i = 0 , j = N - 1 ; i <= j; i++, j--)
System.out.print( "(" + arr[i] + ", " + arr[j] + ")" + " " );
}
public static void main (String[] args)
{
int arr[] = { 9 , 6 , 5 , 1 };
int N = arr.length;
findOptimalPairs(arr, N);
}
}
|
Python3
def findOptimalPairs(arr, N):
arr.sort(reverse = False )
i = 0
j = N - 1
while (i < = j):
print ( "(" , arr[i], "," ,
arr[j], ")" , end = " " )
i + = 1
j - = 1
if __name__ = = '__main__' :
arr = [ 9 , 6 , 5 , 1 ]
N = len (arr)
findOptimalPairs(arr, N)
|
C#
using System;
public class GFG{
static void findOptimalPairs( int []arr, int N)
{
Array.Sort(arr);
for ( int i = 0, j = N - 1; i <= j; i++, j--)
Console.Write( "(" + arr[i] + ", " + arr[j] + ")" + " " );
}
static public void Main (){
int []arr = {9, 6, 5, 1};
int N = arr.Length;
findOptimalPairs(arr, N);
}
}
|
PHP
<?php
function findOptimalPairs( $arr , $N )
{
sort( $arr );
for ( $i = 0, $j = $N - 1; $i <= $j ; $i ++, $j --)
echo "(" , $arr [ $i ],
", " , $arr [ $j ], ")" , " " ;
}
$arr = array ( 9, 6, 5, 1 );
$N = sizeof( $arr );
findOptimalPairs( $arr , $N );
?>
|
Javascript
<script>
function findOptimalPairs(arr, N)
{
arr.sort( function (a,b){ return a-b;});
for ( var i = 0, j = N - 1; i <= j; i++, j--)
document.write( "(" + arr[i] + ", " + arr[j] + ")" + " " );
}
var arr = [ 9, 6, 5, 1 ];
var N = arr.length;
findOptimalPairs(arr, N);
</script>
|
Complexity Analysis:
- Time Complexity: O(n*log n) where n is the size of the array.
- Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
19 Aug, 2022
Like Article
Save Article