Generate an N | Java program for the above approach ; 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 ; Driver Code ; Given array ; Size of the array ; Function Call
import java . util . * ; class GFG { static void constructArray ( int arr [ ] , int N ) { int brr [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { int K = ( int ) ( Math . log ( arr [ i ] ) / Math . log ( 2 ) ) ; int R = ( int ) Math . pow ( 2 , K ) ; brr [ i ] = R ; } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( brr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 11 , 5 , 7 , 3 , 2 } ; int N = arr . length ; constructArray ( arr , N ) ; } }