Range Queries for finding the Sum of all even parity numbers | pref [ ] array to precompute the sum of all Even Parity Numbers ; Function that returns true if count of set bits in x is even ; Parity will store the count of set bits ; Function to precompute the sum of all even parity numbers upto 100000 ; isEvenParity ( ) return the number i if i has even parity else return 0 ; Function to print sum for each query ; Function to print sum of all even parity numbers between [ L , R ] ; Function that pre computes the sum of all even parity numbers ; Iterate over all Queries to print sum ; Queries ; Function that print the sum of all even parity numbers in Range [ L , R ]
let pref = new Array ( 100001 ) . fill ( 0 ) ; function isEvenParity ( num ) { let parity = 0 ; let x = num ; while ( x != 0 ) { if ( x & 1 == 1 ) parity ++ ; x = x >> 1 ; } if ( parity % 2 == 0 ) return num ; else return 0 ; } function preCompute ( ) { for ( let i = 1 ; i < 100001 ; i ++ ) { pref [ i ] = pref [ i - 1 ] + isEvenParity ( i ) ; } } function printSum2 ( L , R ) { document . write ( pref [ R ] - pref [ L - 1 ] + " " ) ; } function printSum ( arr , Q ) { preCompute ( ) ; for ( let i = 0 ; i < Q ; i ++ ) { printSum2 ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } } let N = 2 ; let Q = [ [ 1 , 10 ] , [ 121 , 211 ] ] ; printSum ( Q , N ) ;