Queries to calculate difference between the frequencies of the most and least occurring characters in specified substring | C # program for the above approach ; Function to find difference between maximum and minimum frequency of a character in given range ; Stores length of string ; Stores count of queries ; Iterate over the characters of the string ; Stores l - value of a query ; Stores r - value of a query ; Store count of every character laying in range [ l , r ] ; Update frequency of current character ; Stores maximum frequency of characters in given range ; Stores minimum frequency of characters in given range ; Iterate over all possible characters of the given string ; Update mx ; If ( j + ' a ' ) is present ; difference between max and min ; Driver code ; Given string ; Given queries ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void maxDiffFreq ( List < Tuple < int , int > > queries , string S ) { int N = S . Length ; int Q = queries . Count ; for ( int i = 0 ; i < Q ; ++ i ) { int l = queries [ i ] . Item1 - 1 ; int r = queries [ i ] . Item2 - 1 ; int [ ] freq = new int [ 26 ] ; for ( int j = l ; j <= r ; j ++ ) { freq [ S [ j ] - ' a ' ] ++ ; } int mx = 0 ; int mn = 99999999 ; for ( int j = 0 ; j < 26 ; j ++ ) { mx = Math . Max ( mx , freq [ j ] ) ; if ( freq [ j ] != 0 ) mn = Math . Min ( mn , freq [ j ] ) ; } Console . WriteLine ( mx - mn ) ; } } static void Main ( ) { string S = " abaabac " ; List < Tuple < int , int > > queries = new List < Tuple < int , int > > ( ) ; queries . Add ( new Tuple < int , int > ( 2 , 6 ) ) ; queries . Add ( new Tuple < int , int > ( 1 , 7 ) ) ; maxDiffFreq ( queries , S ) ; } }