Count array elements whose highest power of 2 less than or equal to that number is present in the given array | Function to count array elements whose highest power of 2 is less than or equal to that number is present in the given array ; Stores the resultant count of array elements ; Stores frequency of visited array elements ; Traverse the array ; Calculate log base 2 of the element arr [ i ] ; Highest power of 2 whose value is at most arr [ i ] ; Increment the count by 1 ; Return the resultant count ; Driver Code
function countElement ( arr , N ) { let count = 0 ; let m = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( m . has ( arr [ i ] ) ) { m . set ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) } else { m . set ( arr [ i ] , 1 ) } } for ( let i = 0 ; i < N ; i ++ ) { let lg = Math . floor ( Math . log2 ( arr [ i ] ) ) ; let p = Math . pow ( 2 , lg ) ; if ( m . get ( p ) ) { count ++ ; } } return count ; } let arr = [ 3 , 4 , 6 , 9 ] ; let N = arr . length document . write ( countElement ( arr , N ) ) ;