Find the Kth pair in ordered list of all possible sorted pairs of the Array | C ++ program to find the K - th pair in a lexicographically sorted array ; Function to find the k - th pair ; Sorting the array ; Iterating through the array ; Finding the number of same elements ; Checking if N * T is less than the remaining K . If it is , then arr [ i ] is the first element in the required pair ; Printing the K - th pair ; Driver code
#include <bits/stdc++.h> NEW_LINE 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 ) ; }