Find array elements with rightmost set bit at the position of the rightmost set bit in K | Function to find the mask for finding rightmost set bit in K ; Function to find all array elements with rightmost set bit same as that in K ; Stores mask of K ; Store position of rightmost set bit ; Traverse the array ; Check if rightmost set bit of current array element is same as position of rightmost set bit in K ; Input ; Function call to find the elements having same rightmost set bit as of K
function findMask ( K ) { let mask = 1 ; while ( ( K & mask ) == 0 ) { mask = mask << 1 ; } return mask ; } function sameRightSetBitPos ( arr , N , K ) { let mask = findMask ( K ) ; let pos = ( K & mask ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] & mask ) == pos ) document . write ( arr [ i ] + " " ) ; } } let arr = [ 3 , 4 , 6 , 7 , 9 , 12 , 15 ] ; let N = arr . length ; let K = 7 ; sameRightSetBitPos ( arr , N , K ) ;