text: Queries to print the character that occurs the maximum number of times in a given range | C # program to find the sum of the addition of all possible subsets . ; Function that answers all the queries ; Length of the string ; Number of queries ; Prefix array ; Iterate for all the characters ; Increase the count of the character ; Presum array for all 26 characters ; Update the prefix array ; Answer every query ; Range ; Iterate for all characters ; Times the lowercase character j occurs till r - th index ; Subtract the times it occurred till ( l - 1 ) th index ; Max times it occurs ; Print the answer ; Driver Code
code: using System ; class GFG { static void solveQueries ( String str , int [ , ] query ) { int len = str . Length ; int Q = query . GetLength ( 0 ) ; int [ , ] pre = new int [ len , 26 ] ; for ( int i = 0 ; i < len ; i ++ ) { pre [ i , str [ i ] - ' a ' ] ++ ; if ( i > 0 ) { for ( int j = 0 ; j < 26 ; j ++ ) pre [ i , j ] += pre [ i - 1 , j ] ; } } for ( int i = 0 ; i < Q ; i ++ ) { int l = query [ i , 0 ] ; int r = query [ i , 1 ] ; int maxi = 0 ; char c = ' a ' ; for ( int j = 0 ; j < 26 ; j ++ ) { int times = pre [ r , j ] ; if ( l > 0 ) times -= pre [ l - 1 , j ] ; if ( times > maxi ) { maxi = times ; c = ( char ) ( ' a ' + j ) ; } } Console . WriteLine ( " Query " + ( i + 1 ) + " : ▁ " + c ) ; } } public static void Main ( String [ ] args ) { String str = " striver " ; int [ , ] query = { { 0 , 1 } , { 1 , 6 } , { 5 , 6 } } ; solveQueries ( str , query ) ; } }