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 N^{2} sorted pairs of the array **arr[]**.

A pair (p1, q1) is lexicographically smaller than the pair (p2, q2) only if

p1 ≤ p2andq1 < 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, … D
_{1}, D_{2}, D_{3}… D_{N – 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 N
^{2}pairs of elements, the first T^{2}elements will always be {X, X}. - The next T elements will be {X, D
_{2}} and the next T elements will be {X, D_{2}} 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++

`// C++ program to find the K-th pair ` `// in a lexicographically sorted array ` ` ` `#include <bits/stdc++.h> ` `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); ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

1 3

**Time Complexity:** *O(N * log(N))*, where N is the size of the array.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sum of all ordered pair-products from a given array
- Find k ordered pairs in array with minimum difference d
- Sum of maximum and minimum of Kth subset ordered by increasing subset sum
- Swap Kth node from beginning with Kth node from end in a Linked List
- Find Kth number from sorted array formed by multiplying any two numbers in the array
- Generate all possible sorted arrays from alternate elements of two given sorted arrays
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Find the number of ordered pairs such that a * p + b * q = N, where p and q are primes
- Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M
- Find the Kth smallest element in the sorted generated array
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- Kth array element after M replacements of array elements by XOR of adjacent pairs
- Multiplication table till N rows where every Kth row is table of K upto Kth term
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Count ordered pairs of positive numbers such that their sum is S and XOR is K
- Count number of ordered pairs with Even and Odd Sums
- Count number of ordered pairs with Even and Odd Product
- Count ordered pairs with product less than N
- Count of Ordered Pairs (X, Y) satisfying the Equation 1/X + 1/Y = 1/N
- Count ordered pairs of numbers with a given LCM

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.