Generate an N | C # 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 ] ; Print array elements ; Driver Code ; Given array ; Size of the array ; Function Call
using System ; class GFG { static void constructArray ( int [ ] arr , int N ) { int [ ] brr = new int [ N ] ; Array . Clear ( brr , 0 , brr . Length ) ; 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 ++ ) { Console . Write ( brr [ i ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 11 , 5 , 7 , 3 , 2 } ; int N = arr . Length ; constructArray ( arr , N ) ; } }