Queries to find Kth greatest character in a range [ L , R ] from a string with updates | Function to find the kth greatest character from the strijng ; Sorting the String in non - increasing Order ; Function to print the K - th character from the subString S [ l ] . . S [ r ] ; 0 - based indexing ; SubString of str from the indices l to r . ; Extract kth Largest character ; Function to replace character at pos of str by the character s ; Index of S to be updated . ; Character to be replaced at index in S ; Given String ; Count of queries ; Queries
function find_kth_largest ( str , k ) { str . sort ( ) ; reverse ( str ) ; return str [ k - 1 ] ; } function reverse ( a ) { let i , n = a . length ; let t ; for ( i = 0 ; i < Math . floor ( n / 2 ) ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } function printCharacter ( str , l , r , k ) { l = l - 1 ; r = r - 1 ; let temp = str . substring ( l , r - l + 1 ) ; let ans = find_kth_largest ( temp . split ( " " ) , k ) ; return ans ; } function updateString ( str , pos , s ) { let index = pos - 1 ; let c = s ; str [ index ] = c ; } let str = " " ; let Q = 3 ; document . write ( printCharacter ( str , 1 , 2 , 2 ) + " " ) ; updateString ( str . split ( " " ) , 4 , ' ' ) ; document . write ( printCharacter ( str , 1 , 5 , 4 ) + " " ) ;