Find the k | 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
function kth_String ( n , k ) { for ( i = n - 2 ; i >= 0 ; i -- ) { if ( k <= ( n - i - 1 ) ) { for ( j = 0 ; j < n ; j ++ ) { if ( j == i j == n - k ) document . write ( ' ' ) ; else document . write ( ' ' ) ; } break ; } k -= ( n - i - 1 ) ; } } var n = 5 , k = 7 ; kth_String ( n , k ) ;