Queries to count characters having odd frequency in a range [ L , R ] | Function to print the number of characters having odd frequencies for each query ; A function to construct the [ ] arr and prefix [ ] ; Stores array length ; Stores the unique powers of 2 associated to each character ; Prefix array to store the XOR values from array elements ;  ; Driver Code
function queryResult ( prefix , Q ) { let l = Q [ 0 ] ; let r = Q [ 1 ] ; if ( l == 0 ) { let xorval = prefix [ r ] ; document . write ( countSetBits ( xorval ) + " " ) ; } else { let xorval = prefix [ r ] ^ prefix [ l - 1 ] ; document . write ( countSetBits ( xorval ) + " " ) ; } } function calculateCount ( S , Q , m ) { let n = S . length ; let arr = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { arr [ i ] = ( 1 << ( S [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) ) ; } let prefix = new Array ( n ) ; let x = 0 ; for ( let i = 0 ; i < n ; i ++ ) { x ^= arr [ i ] ; prefix [ i ] = x ; } for ( let i = 0 ; i < m ; i ++ ) { queryResult ( prefix , Q [ i ] ) ; } } function countSetBits ( x ) { let setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } let S = " " ; let Q = [ [ 2 , 4 ] , [ 0 , 3 ] , [ 0 , 12 ] ] ; calculateCount ( S , Q , 3 ) ;