Generate an N | Python3 program for the above approach ; Function to construct an 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 ] ; Prarray elements ; Driver Code ; Given array ; Size of the array ; Function Call
from math import log NEW_LINE def constructArray ( arr , N ) : NEW_LINE INDENT brr = [ 0 ] * N NEW_LINE for i in range ( N ) : NEW_LINE INDENT K = int ( log ( arr [ i ] ) / log ( 2 ) ) NEW_LINE R = pow ( 2 , K ) NEW_LINE brr [ i ] = R NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT print ( brr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 11 , 5 , 7 , 3 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE constructArray ( arr , N ) NEW_LINE DEDENT