Generate an N | Function to conan array with given conditions ; Traverse the array arr ; Stores closest power of 2 less than or equal to arr [ i ] ; Stores R into brr [ i ] ; Print array elements ; Given array ; Size of the array ; Function Call
function constructArray ( arr , N ) { var brr = Array ( N ) . fill ( 0 ) ; for ( i = 0 ; i < N ; i ++ ) { var K = parseInt ( ( Math . log ( arr [ i ] ) / Math . log ( 2 ) ) ) ; var R = parseInt ( Math . pow ( 2 , K ) ) ; brr [ i ] = R ; } for ( i = 0 ; i < N ; i ++ ) { document . write ( brr [ i ] + " " ) ; } } var arr = [ 11 , 5 , 7 , 3 , 2 ] ; var N = arr . length ; constructArray ( arr , N ) ;