Number of visible boxes after putting one inside another | Java program to count number of visible boxes . ; return the minimum number of visible boxes ; New Queue of integers . ; sorting the array ; traversing the array ; checking if current element is greater than or equal to twice of front element ; Pushing each element of array ; Driver code
import java . util . LinkedList ; import java . util . Queue ; import java . util . Arrays ; public class GFG { static int minimumBox ( int [ ] arr , int n ) { Queue < Integer > q = new LinkedList < > ( ) ; Arrays . sort ( arr ) ; q . add ( arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { int now = q . element ( ) ; if ( arr [ i ] >= 2 * now ) q . remove ( ) ; q . add ( arr [ i ] ) ; } return q . size ( ) ; } public static void main ( String args [ ] ) { int [ ] arr = { 4 , 1 , 2 , 8 } ; int n = arr . length ; System . out . println ( minimumBox ( arr , n ) ) ; } }