Minimize maximum array element by splitting array elements into powers of two at most K times | Function to find the minimum value of the maximum element of the array by splitting at most K array element into perfect powers of 2 ; Sort the array element in the ascending order ; Reverse the array ; If count of 0 is equal to N ; Otherwise , if K is greater than or equal to N ; Otherwise ; Driver code
function minimumSize ( arr , N , K ) { ( arr ) . sort ( function ( a , b ) { return a - b ; } ) ; reverse ( arr ) ; if ( count ( arr , 0 ) == N ) document . write ( 0 ) ; else if ( K >= N ) document . write ( 1 ) ; else document . write ( arr [ K ] ) ; } function reverse ( a ) { let i , k , t ; let n = a . length ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } } function count ( a , n ) { let freq = 0 ; for ( let i = 0 ; i < a . length ; i ++ ) { if ( a [ i ] == n ) freq ++ ; } return freq ; } let arr = [ 2 , 4 , 8 , 2 ] ; let K = 2 ; let N = arr . length ; minimumSize ( arr , N , K ) ;