Find k ordered pairs in array with minimum difference d
Last Updated :
23 Jun, 2022
Given an array arr[] and two integers K and D, the task is to find exactly K pairs (arr[i], arr[j]) from the array such that |arr[i] – arr[j]| ? D and i != j. If it is impossible to get such pairs then print -1. Note that a single element can only participate in a single pair.
Examples:
Input: arr[] = {4, 6, 10, 23, 14, 7, 2, 20, 9}, K = 4, D = 3
Output:
(2, 10)
(4, 14)
(6, 20)
(7, 23)
Input: arr[] = {2, 10, 4, 6, 12, 5, 7, 3, 1, 9}, K = 5, D = 10
Output : -1
Approach: If we had to find only 1 pair then we would have checked only the maximum and minimum element from the array. Similarly, to get K pairs we can compare minimum K elements with the corresponding maximum K elements. Sorting can be used to get the minimum and maximum elements.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findPairs( int arr[], int n, int k, int d)
{
if (n < 2 * k) {
cout << -1;
return ;
}
vector<pair< int , int > > pairs;
sort(arr, arr + n);
for ( int i = 0; i < k; i++) {
if (arr[n - k + i] - arr[i] >= d) {
pair< int , int > p = make_pair(arr[i], arr[n - k + i]);
pairs.push_back(p);
}
}
if (pairs.size() < k) {
cout << -1;
return ;
}
for ( auto v : pairs) {
cout << "(" << v.first << ", "
<< v.second << ")" << endl;
}
}
int main()
{
int arr[] = { 4, 6, 10, 23, 14, 7, 2, 20, 9 };
int n = sizeof (arr) / sizeof (arr[0]);
int k = 4, d = 3;
findPairs(arr, n, k, d);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class pair
{
int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static void findPairs( int arr[], int n,
int k, int d)
{
if (n < 2 * k)
{
System.out.print(- 1 );
return ;
}
Vector<pair> pairs = new Vector<pair>();
Arrays.sort(arr);
for ( int i = 0 ; i < k; i++)
{
if (arr[n - k + i] - arr[i] >= d)
{
pair p = new pair(arr[i],
arr[n - k + i]);
pairs.add(p);
}
}
if (pairs.size() < k)
{
System.out.print(- 1 );
return ;
}
for (pair v : pairs)
{
System.out.println( "(" + v.first +
", " + v.second + ")" );
}
}
public static void main(String[] args)
{
int arr[] = { 4 , 6 , 10 , 23 , 14 , 7 , 2 , 20 , 9 };
int n = arr.length;
int k = 4 , d = 3 ;
findPairs(arr, n, k, d);
}
}
|
Python3
def findPairs(arr, n, k, d):
if (n < 2 * k):
print ( "-1" )
return
pairs = []
arr = sorted (arr)
for i in range (k):
if (arr[n - k + i] - arr[i] > = d):
pairs.append([arr[i], arr[n - k + i]])
if ( len (pairs) < k):
print ( "-1" )
return
for v in pairs:
print ( "(" ,v[ 0 ], ", " ,v[ 1 ], ")" )
arr = [ 4 , 6 , 10 , 23 , 14 , 7 , 2 , 20 , 9 ]
n = len (arr)
k = 4
d = 3
findPairs(arr, n, k, d)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public class pair
{
public int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static void findPairs( int []arr, int n,
int k, int d)
{
if (n < 2 * k)
{
Console.Write(-1);
return ;
}
List<pair> pairs = new List<pair>();
Array.Sort(arr);
for ( int i = 0; i < k; i++)
{
if (arr[n - k + i] - arr[i] >= d)
{
pair p = new pair(arr[i],
arr[n - k + i]);
pairs.Add(p);
}
}
if (pairs.Count < k)
{
Console.Write(-1);
return ;
}
foreach (pair v in pairs)
{
Console.WriteLine ( "(" + v.first +
", " + v.second + ")" );
}
}
public static void Main(String[] args)
{
int []arr = { 4, 6, 10, 23,
14, 7, 2, 20, 9 };
int n = arr.Length;
int k = 4, d = 3;
findPairs(arr, n, k, d);
}
}
|
Javascript
<script>
function findPairs(arr, n, k, d) {
if (n < 2 * k) {
document.write(-1);
return ;
}
let pairs = [];
arr.sort((a, b) => a - b);
for (let i = 0; i < k; i++) {
if (arr[n - k + i] - arr[i] >= d) {
let p = [arr[i], arr[n - k + i]];
pairs.push(p);
}
}
if (pairs.length < k) {
document.write(-1);
return ;
}
for (let v of pairs) {
document.write( "(" + v[0] + ", " + v[1] + ")" + "<br>" );
}
}
let arr = [4, 6, 10, 23, 14, 7, 2, 20, 9];
let n = arr.length;
let k = 4, d = 3;
findPairs(arr, n, k, d);
</script>
|
Output:
(2, 10)
(4, 14)
(6, 20)
(7, 23)
Time Complexity: O(n*log(n))
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...