Find the Kth pair in ordered list of all possible sorted pairs of the Array
Given an array arr[] containing N integers and a number K, the task is to find the K-th pair in the ordered list of all possible N2 sorted pairs of the array arr[].
A pair (p1, q1) is lexicographically smaller than the pair (p2, q2) only if p1 ? p2 and q1 < q2.
Examples:
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}
Approach: Naturally, K-th sorted pair from all possible set of pairs will be {arr[K/N], arr[K%N]}. But, this method works only if all the elements in the array are unique. Therefore, the following steps are followed to make the array behave like a unique array:
- Let the array arr[] be {X, X, X, … D1, D2, D3 … DN – T}.
- Here, let’s assume the number of repeating elements in the array to be T and the element which is being repeated be X. So, the number of distinct elements in the array is (N – T).
- Now, from the first N * T pairs out of N2 pairs of elements, the first T2 elements will always be {X, X}.
- The next T elements will be {X, D2} and the next T elements will be {X, D2} and so on.
- So, if we need to find the K-th element, subtract N * T from K and skip the first T same elements.
- Repeat the above process until K becomes less than N * T.
- At this step, the first element in the pair would be the counter variable ‘i’. The second element would be the remaining K-th element from the remaining elements which is K / T. So, the required answer is {arr[i], arr[K/T]}.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void kthpair( int n, int k, int arr[])
{
int i, t;
sort(arr, arr + n);
--k;
for (i = 0; i < n; i += t) {
for (t = 1; arr[i] == arr[i + t]; ++t)
;
if (t * n > k)
break ;
k = k - t * n;
}
cout << arr[i] << ' ' << arr[k / t];
}
int main()
{
int n = 3, k = 2;
int arr[n] = { 3, 1, 5 };
kthpair(n, k, arr);
}
|
Java
import java.util.*;
class GFG{
static void kthpair( int n, int k,
int arr[])
{
int i, t = 0 ;
Arrays.sort(arr);
--k;
for (i = 0 ; i < n; i += t)
{
for (t = 1 ; arr[i] == arr[i + t]; ++t)
;
if (t * n > k)
break ;
k = k - t * n;
}
System.out.print(arr[i] + " " +
arr[k / t]);
}
public static void main(String[] args)
{
int n = 3 , k = 2 ;
int arr[] = { 3 , 1 , 5 };
kthpair(n, k, arr);
}
}
|
Python3
def kthpair(n, k, arr):
arr.sort()
k - = 1
i = 0
while (i < n):
t = 1
while (arr[i] = = arr[i + t]):
t + = 1
if (t * n > k):
break
k = k - t * n
i + = t
print (arr[i], " " , arr[k / / t])
if __name__ = = "__main__" :
n, k = 3 , 2
arr = [ 3 , 1 , 5 ]
kthpair(n, k, arr)
|
C#
using System;
class GFG{
static void kthpair( int n, int k,
int [] arr)
{
int i, t = 0;
Array.Sort(arr);
--k;
for (i = 0; i < n; i += t)
{
for (t = 1; arr[i] == arr[i + t]; ++t);
if (t * n > k)
break ;
k = k - t * n;
}
Console.Write(arr[i] + " " + arr[k / t]);
}
static public void Main ()
{
int n = 3, k = 2;
int [] arr = { 3, 1, 5 };
kthpair(n, k, arr);
}
}
|
Javascript
<script>
function kthpair(n,k,arr)
{
let i, t = 0;
arr.sort();
--k;
for (i = 0; i < n; i += t)
{
for (t = 1; arr[i] == arr[i + t]; ++t)
;
if (t * n > k)
break ;
k = k - t * n;
}
document.write(arr[i] + " " +
arr[k / t]);
}
let n = 3, k = 2;
let arr =[ 3, 1, 5 ];
kthpair(n, k, arr);
</script>
|
Time Complexity: O(N * log(N)), where N is the size of the array.
Auxiliary Space: O(1)
Last Updated :
15 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...