Find the k | Java 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 eft most Y ; If i is the left most position of Y ; Put Y in their positions ; Driver code ; Function call
class GFG { static 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 j == n - k ) System . out . print ( ' Y ' ) ; else System . out . print ( ' X ' ) ; } break ; } k -= ( n - i - 1 ) ; } } public static void main ( String [ ] args ) { int n = 5 , k = 7 ; kth_String ( n , k ) ; } }