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 ; Driver Code ; Input ; Function call to find the elements having same rightmost set bit as of K
def findMask ( K ) : NEW_LINE INDENT mask = 1 ; NEW_LINE while ( ( K & mask ) == 0 ) : NEW_LINE INDENT mask = mask << 1 ; NEW_LINE DEDENT return mask ; NEW_LINE DEDENT def sameRightSetBitPos ( arr , N , K ) : NEW_LINE INDENT mask = findMask ( K ) ; NEW_LINE pos = ( K & mask ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( ( arr [ i ] & mask ) == pos ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 4 , 6 , 7 , 9 , 12 , 15 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE K = 7 ; NEW_LINE sameRightSetBitPos ( arr , N , K ) ; NEW_LINE DEDENT