给定一个包含N 个整数和一个数字K的数组arr[] ,任务是在数组arr[]的所有可能的 N 2 个排序对的有序列表中找到第 K 对。
A pair (p1, q1) is lexicographically smaller than the pair (p2, q2) only if p1 ≤ p2 and q1 < q2.
例子:
Input: arr[] = {2, 1}, K = 4
Output: {2, 2}
Explanation:
The sorted sequence for the given array is {1, 1}, {1, 2}, {2, 1}, {2, 2}. So the 4th pair is {2, 2}.
Input: arr[] = {3, 1, 5}, K = 2
Output: {1, 3}
方法:自然地,所有可能的对集中的第 K 个排序对将是{arr[K/N], arr[K%N]} 。但是,此方法仅在数组中的所有元素都是唯一的情况下才有效。因此,遵循以下步骤使数组表现得像一个唯一数组:
- 令数组 arr[] 为 {X, X, X, … D 1 , D 2 , D 3 … D N – T }。
- 在这里,让我们假设数组中重复元素的数量为 T,重复元素的数量为 X。因此,数组中不同元素的数量为 (N – T)。
- 现在,从 N 2对元素中的前 N T 对开始,前 T 2元素将始终是 {X, X}。
- 接下来的 T 个元素将是 {X, D 2 },接下来的 T 个元素将是 {X, D 2 },依此类推。
- 所以,如果我们需要找到第 K 个元素,从 K 中减去 N * T 并跳过前 T 个相同的元素。
- 重复上述过程,直到 K 小于 N * T。
- 在这一步,该对中的第一个元素将是计数器变量“i”。第二个元素将是剩余元素中剩余的第 K 个元素,即 K/T。因此,所需的答案是 {arr[i], arr[K/T]}。
下面是上述方法的实现:
C++
// C++ program to find the K-th pair
// in a lexicographically sorted array
#include
using namespace std;
// Function to find the k-th pair
void kthpair(int n, int k, int arr[])
{
int i, t;
// Sorting the array
sort(arr, arr + n);
--k;
// Iterating through the array
for (i = 0; i < n; i += t) {
// Finding the number of same elements
for (t = 1; arr[i] == arr[i + t]; ++t)
;
// Checking if N*T is less than the
// remaining K. If it is, then arr[i]
// is the first element in the required
// pair
if (t * n > k)
break;
k = k - t * n;
}
// Printing the K-th pair
cout << arr[i] << ' ' << arr[k / t];
}
// Driver code
int main()
{
int n = 3, k = 2;
int arr[n] = { 3, 1, 5 };
kthpair(n, k, arr);
}
Java
// Java program to find the K-th pair
// in a lexicographically sorted array
import java.util.*;
class GFG{
// Function to find the k-th pair
static void kthpair(int n, int k,
int arr[])
{
int i, t = 0;
// Sorting the array
Arrays.sort(arr);
--k;
// Iterating through the array
for (i = 0; i < n; i += t)
{
// Finding the number of same elements
for (t = 1; arr[i] == arr[i + t]; ++t)
;
// Checking if N*T is less than the
// remaining K. If it is, then arr[i]
// is the first element in the required
// pair
if (t * n > k)
break;
k = k - t * n;
}
// Printing the K-th pair
System.out.print(arr[i] + " " +
arr[k / t]);
}
// Driver code
public static void main(String[] args)
{
int n = 3, k = 2;
int arr[] = { 3, 1, 5 };
kthpair(n, k, arr);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to find the K-th pair
# in a lexicographically sorted array
# Function to find the k-th pair
def kthpair(n, k, arr):
# Sorting the array
arr.sort()
k -= 1
# Iterating through the array
i = 0
while (i < n):
# Finding the number of same elements
t = 1
while (arr[i] == arr[i + t]):
t += 1
# Checking if N*T is less than the
# remaining K. If it is, then arr[i]
# is the first element in the required
# pair
if (t * n > k):
break
k = k - t * n
i += t
# Printing the K-th pair
print(arr[i], " ", arr[k // t])
# Driver code
if __name__ == "__main__":
n, k = 3, 2
arr = [ 3, 1, 5 ]
kthpair(n, k, arr)
# This code is contributed by chitranayal
C#
// C# program to find the K-th pair
// in a lexicographically sorted array
using System;
class GFG{
// Function to find the k-th pair
static void kthpair(int n, int k,
int[] arr)
{
int i, t = 0;
// Sorting the array
Array.Sort(arr);
--k;
// Iterating through the array
for(i = 0; i < n; i += t)
{
// Finding the number of same elements
for(t = 1; arr[i] == arr[i + t]; ++t);
// Checking if N*T is less than the
// remaining K. If it is, then arr[i]
// is the first element in the required
// pair
if (t * n > k)
break;
k = k - t * n;
}
// Printing the K-th pair
Console.Write(arr[i] + " " + arr[k / t]);
}
// Driver code
static public void Main ()
{
int n = 3, k = 2;
int[] arr = { 3, 1, 5 };
kthpair(n, k, arr);
}
}
// This code is contributed by ShubhamCoder
Javascript
输出:
1 3
时间复杂度: O(N * log(N)) ,其中 N 是数组的大小。
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live