Find the k | C ++ program find the Kth string in lexicographical order consisting of N - 2 xs and 2 ys ; Function to find the Kth string in lexicographical order which consists of N - 2 xs and 2 ys ; Iterate for all possible positions of Left most Y ; If i is the left most position of Y ; Put Y in their positions ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void kth_string ( int n , int k ) { for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( k <= ( n - i - 1 ) ) { for ( int j = 0 ; j < n ; j ++ ) { if ( j == i or j == n - k ) cout << ' Y ' ; else cout << ' X ' ; } break ; } k -= ( n - i - 1 ) ; } } int main ( ) { int n = 5 , k = 7 ; kth_string ( n , k ) ; return 0 ; }