Queries to find Kth greatest character in a range [ L , R ] from a string with updates | C ++ Program to implement the above approach ; 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 ; Driver Code ; Given string ; Count of queries ; Queries
#include " bits / stdc + + . h " NEW_LINE using namespace std ; char find_kth_largest ( string str , int k ) { sort ( str . begin ( ) , str . end ( ) , greater < char > ( ) ) ; return str [ k - 1 ] ; } char printCharacter ( string str , int l , int r , int k ) { l = l - 1 ; r = r - 1 ; string temp = str . substr ( l , r - l + 1 ) ; char ans = find_kth_largest ( temp , k ) ; return ans ; } void updateString ( string str , int pos , char s ) { int index = pos - 1 ; char c = s ; str [ index ] = c ; } int main ( ) { string str = " abcddef " ; int Q = 3 ; cout << printCharacter ( str , 1 , 2 , 2 ) << endl ; updateString ( str , 4 , ' g ' ) ; cout << printCharacter ( str , 1 , 5 , 4 ) << endl ; return 0 ; }