给定包含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
输出:
1 3
时间复杂度: O(N * log(N)) ,其中N是数组的大小。