Push all the composite numbers from the array to the heaps
for ( int i = 0 ; i < n ; i ++ ) { if ( ! prime [ arr [ i ] ] ) { minHeap . add ( arr [ i ] ) ; maxHeap . add ( arr [ i ] ) ; } } long minProduct = 1 , maxProduct = 1 , minSum = 0 , maxSum = 0 ; Integer lastMin = 0 , lastMax = 0 ; while ( k -- > 0 ) { if ( minHeap . peek ( ) != null || maxHeap . peek ( ) != null ) {