Range Queries for finding the Sum of all even parity numbers | Java program to find the sum of all Even Parity numbers in the given range ; 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 ; Driver code ; Queries ; Function that print the sum of all even parity numbers in Range [ L , R ]
import java . io . * ; import java . util . * ; class GFG { static int [ ] pref = new int [ 100001 ] ; static int isEvenParity ( int num ) { int parity = 0 ; int x = num ; while ( x != 0 ) { if ( ( x & 1 ) == 1 ) parity ++ ; x = x >> 1 ; } if ( parity % 2 == 0 ) return num ; else return 0 ; } static void preCompute ( ) { for ( int i = 1 ; i < 100001 ; i ++ ) { pref [ i ] = pref [ i - 1 ] + isEvenParity ( i ) ; } } static void printSum ( int L , int R ) { System . out . println ( pref [ R ] - pref [ L - 1 ] ) ; } static void printSum ( int arr [ ] [ ] , int Q ) { preCompute ( ) ; for ( int i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } } public static void main ( String [ ] args ) { int N = 2 ; int [ ] [ ] Q = { { 1 , 10 } , { 121 , 211 } } ; printSum ( Q , N ) ; } }