Find the element having maximum set bits in the given range for Q queries | C ++ implementation to find maximum set bits value in a range ; Structure to store two values in one node ; Function that returns the count of set bits in a number ; Parity will store the count of set bits ; Function to build the segment tree ; Condition to check if there is only one element in the array ; If there are more than one elements , then recur for left and right subtrees ; Condition to check the maximum set bits is greater in two subtrees ; Condition when maximum set bits are equal in both subtrees ; Function to do the range query in the segment tree ; If segment of this node is outside the given range , then return the minimum value . ; If segment of this node is a part of given range , then return the node of the segment ; If left segment of this node falls out of range , then recur in the right side of the tree ; If right segment of this node falls out of range , then recur in the left side of the tree ; If a part of this segment overlaps with the given range ; Returns the value ; Driver code ; Calculates the length of array ; Build Segment Tree ; Find the max set bits value between 1 st and 4 th index of array ; Find the max set bits value between 0 th and 2 nd index of array
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int value ; int max_set_bits ; } ; Node tree [ 4 * 10000 ] ; int setBits ( int x ) { int parity = 0 ; while ( x != 0 ) { if ( x & 1 ) parity ++ ; x = x >> 1 ; } return parity ; } void buildSegmentTree ( int a [ ] , int index , int beg , int end ) { if ( beg == end ) { tree [ index ] . value = a [ beg ] ; tree [ index ] . max_set_bits = setBits ( a [ beg ] ) ; } else { int mid = ( beg + end ) / 2 ; buildSegmentTree ( a , 2 * index + 1 , beg , mid ) ; buildSegmentTree ( a , 2 * index + 2 , mid + 1 , end ) ; if ( tree [ 2 * index + 1 ] . max_set_bits > tree [ 2 * index + 2 ] . max_set_bits ) { tree [ index ] . max_set_bits = tree [ 2 * index + 1 ] . max_set_bits ; tree [ index ] . value = tree [ 2 * index + 1 ] . value ; } else if ( tree [ 2 * index + 2 ] . max_set_bits > tree [ 2 * index + 1 ] . max_set_bits ) { tree [ index ] . max_set_bits = tree [ 2 * index + 2 ] . max_set_bits ; tree [ index ] . value = tree [ 2 * index + 2 ] . value ; } else { tree [ index ] . max_set_bits = tree [ 2 * index + 2 ] . max_set_bits ; tree [ index ] . value = max ( tree [ 2 * index + 2 ] . value , tree [ 2 * index + 1 ] . value ) ; } } } Node query ( int index , int beg , int end , int l , int r ) { Node result ; result . value = result . max_set_bits = -1 ; if ( beg > r end < l ) return result ; if ( beg >= l && end <= r ) return tree [ index ] ; int mid = ( beg + end ) / 2 ; if ( l > mid ) return query ( 2 * index + 2 , mid + 1 , end , l , r ) ; if ( r <= mid ) return query ( 2 * index + 1 , beg , mid , l , r ) ; Node left = query ( 2 * index + 1 , beg , mid , l , r ) ; Node right = query ( 2 * index + 2 , mid + 1 , end , l , r ) ; if ( left . max_set_bits > right . max_set_bits ) { result . max_set_bits = left . max_set_bits ; result . value = left . value ; } else if ( right . max_set_bits > left . max_set_bits ) { result . max_set_bits = right . max_set_bits ; result . value = right . value ; } else { result . max_set_bits = left . max_set_bits ; result . value = max ( right . value , left . value ) ; } return result ; } int main ( ) { int a [ ] = { 18 , 9 , 8 , 15 , 14 , 5 } ; int N = sizeof ( a ) / sizeof ( a [ 0 ] ) ; buildSegmentTree ( a , 0 , 0 , N - 1 ) ; cout << query ( 0 , 0 , N - 1 , 1 , 4 ) . value << endl ; cout << query ( 0 , 0 , N - 1 , 0 , 2 ) . value << endl ; return 0 ; }