Minimize maximum array element by splitting array elements into powers of two at most K times | Java program for the above approach ; 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
import java . lang . * ; import java . util . * ; class GFG { static void minimumSize ( int arr [ ] , int N , int K ) { Arrays . sort ( arr ) ; reverse ( arr ) ; if ( count ( arr , 0 ) == N ) System . out . println ( 0 ) ; else if ( K >= N ) System . out . println ( 1 ) ; else System . out . println ( arr [ K ] ) ; } static void reverse ( int [ ] a ) { int i , k , t ; int n = a . length ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } } static int count ( int [ ] a , int n ) { int freq = 0 ; for ( int i = 0 ; i < a . length ; i ++ ) { if ( a [ i ] == n ) freq ++ ; } return freq ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 8 , 2 } ; int K = 2 ; int N = arr . length ; minimumSize ( arr , N , K ) ; } }