给定一个数组arr []以及两个整数K和D ,任务是从数组中精确找到K对(arr [i],arr [j]) ,使得| arr [i] – arr [j] | ≥D且i!= j 。如果不可能得到这样的对,则打印-1 。请注意,单个元素只能参与单个对。
例子:
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
方法:如果我们只需要找到1对,那么我们将只检查数组中的最大和最小元素。同样,要获得K对,我们可以将最小K个元素与相应的最大K个元素进行比较。排序可用于获取最小和最大元素。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to find the required pairs
void findPairs(int arr[], int n, int k, int d)
{
// There has to be atleast 2*k elements
if (n < 2 * k) {
cout << -1;
return;
}
// To store the pairs
vector > pairs;
// Sort the given array
sort(arr, arr + n);
// For every possible pair
for (int i = 0; i < k; i++) {
// If the current pair is valid
if (arr[n - k + i] - arr[i] >= d) {
// Insert it into the pair vector
pair p = make_pair(arr[i], arr[n - k + i]);
pairs.push_back(p);
}
}
// If k pairs are not possible
if (pairs.size() < k) {
cout << -1;
return;
}
// Print the pairs
for (auto v : pairs) {
cout << "(" << v.first << ", "
<< v.second << ")" << endl;
}
}
// Driver code
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
// Java implementation of the approach
import java.util.*;
class GFG
{
static class pair
{
int first, second;
public pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
// Function to find the required pairs
static void findPairs(int arr[], int n,
int k, int d)
{
// There has to be atleast 2*k elements
if (n < 2 * k)
{
System.out.print(-1);
return;
}
// To store the pairs
Vector pairs = new Vector();
// Sort the given array
Arrays.sort(arr);
// For every possible pair
for (int i = 0; i < k; i++)
{
// If the current pair is valid
if (arr[n - k + i] - arr[i] >= d)
{
// Insert it into the pair vector
pair p = new pair(arr[i],
arr[n - k + i]);
pairs.add(p);
}
}
// If k pairs are not possible
if (pairs.size() < k)
{
System.out.print(-1);
return;
}
// Print the pairs
for (pair v : pairs)
{
System.out.println("(" + v.first +
", " + v.second + ")");
}
}
// Driver code
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);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the approach
# Function to find the required pairs
def findPairs(arr, n, k, d):
# There has to be atleast 2*k elements
if (n < 2 * k):
print("-1")
return
# To store the pairs
pairs=[]
# Sort the given array
arr=sorted(arr)
# For every possible pair
for i in range(k):
# If the current pair is valid
if (arr[n - k + i] - arr[i] >= d):
# Insert it into the pair vector
pairs.append([arr[i], arr[n - k + i]])
# If k pairs are not possible
if (len(pairs) < k):
print("-1")
return
# Print the pairs
for v in pairs:
print("(",v[0],", ",v[1],")")
# Driver code
arr = [4, 6, 10, 23, 14, 7, 2, 20, 9]
n = len(arr)
k = 4
d = 3
findPairs(arr, n, k, d)
# This code is contributed by mohit kumar 29
C#
// C# implementation of the approach
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;
}
}
// Function to find the required pairs
static void findPairs(int []arr, int n,
int k, int d)
{
// There has to be atleast 2*k elements
if (n < 2 * k)
{
Console.Write(-1);
return;
}
// To store the pairs
List pairs = new List();
// Sort the given array
Array.Sort(arr);
// For every possible pair
for (int i = 0; i < k; i++)
{
// If the current pair is valid
if (arr[n - k + i] - arr[i] >= d)
{
// Insert it into the pair vector
pair p = new pair(arr[i],
arr[n - k + i]);
pairs.Add(p);
}
}
// If k pairs are not possible
if (pairs.Count < k)
{
Console.Write(-1);
return;
}
// Print the pairs
foreach (pair v in pairs)
{
Console.WriteLine ("(" + v.first +
", " + v.second + ")");
}
}
// Driver code
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);
}
}
// This code is contributed by PrinciRaj1992
输出:
(2, 10)
(4, 14)
(6, 20)
(7, 23)