Find Kth largest string from the permutations of the string with two characters | Function to print the kth largest string ; loop to iterate through series ; total takes the position of second y ; i takes the position of first y ; calculating first y position ; calculating second y position from first y ; print all x before first y ; print first y ; print all x between first y and second y ; print second y ; print x which occur after second y ; Driver code
function kthString ( n , k ) { var total = 0 ; var i = 1 ; while ( total < k ) { total = total + n - i ; i ++ ; } var first_y_position = i - 1 ; var second_y_position = k - ( total - n + first_y_position ) ; for ( var j = 1 ; j < first_y_position ; j ++ ) document . write ( " " ) ; document . write ( " " ) ; var j = first_y_position + 1 ; while ( second_y_position > 1 ) { document . write ( " " ) ; second_y_position -- ; j ++ ; } document . write ( " " ) ; while ( j < n ) { document . write ( " " ) ; j ++ ; } } var n = 5 ; var k = 7 ; kthString ( n , k ) ;