Count remaining array elements after reversing binary representation of each array element | Function to reverse the binary representation of a number ; Traverse bits of N from the right ; Bitwise left shift ' rev ' by 1 ; If current bit is '1' ; Bitwise right shift N by 1 ; Required number ; Function to count elements from the original array that are also present in the array formed by reversing the binary representation of each element ; Stores the reversed num ; Iterate from [ 0 , N ] ; Stores the presence of integer ; Stores count of elements present in original array ; Traverse the array ; If current number is present ; Print the answer ; Driver Code
function findReverse ( N ) { let rev = 0 ; while ( N > 0 ) { rev <<= 1 ; if ( N & 1 == 1 ) rev ^= 1 ; N >>= 1 ; } return rev ; } function countElements ( arr , N ) { let ans = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { ans . push ( findReverse ( arr [ i ] ) ) ; } let cnt = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { cnt [ arr [ i ] ] = 1 ; } let count = 0 ; for ( let i = 0 ; i < ans . length ; i ++ ) { if ( cnt [ ans [ i ] ] ) count ++ ; } document . write ( count , ' ' ) ; } let arr = [ 1 , 30 , 3 , 8 , 12 ] ; let N = arr . length ; countElements ( arr , N ) ;