# Number of visible boxes after putting one inside another

• Difficulty Level : Medium
• Last Updated : 06 May, 2021

Given N boxes and their size in an array. You are allowed to keep a box inside another box only if the box in which it is held is empty and the size of the box is at least twice as large as the size of the box. The task is to find minimum number of visible boxes.
Examples –

Input : arr[] = { 1, 3, 4, 5 }
Output : 3
Put box of size 1 in box of size 3.

Input : arr[] = { 4, 2, 1, 8 }
Output : 1
Put box of size 1 in box of size 2
and box of size 2 in box of size 4.
And put box of size 4 in box of size 8.

The idea is to sort the array. Now, make a queue and insert first element of sorted array. Now traverse the array from first element and insert each element in the queue, also check if front element of queue is less than or equal to half of current traversed element. So, the number of visible box will be number of element in queue after traversing the sorted array. Basically, we are trying to put a box of size in smallest box which is greater than or equal to 2*x.
For example, if arr[] = { 2, 3, 4, 6 }, then we try to put box of size 2 in box of size 4 instead of box of size 6 because if we put box of size 2 in box of size 6 then box of size 3 cannot be kept in any other box and we need to minimize the number of visible box.

## C++

 // CPP program to count number of visible boxes.#include using namespace std; // return the minimum number of visible boxesint minimumBox(int arr[], int n){    queue q;     // sorting the array    sort(arr, arr + n);     q.push(arr[0]);     // traversing the array    for (int i = 1; i < n; i++)  {         int now = q.front();         // checking if current element        // is greater than or equal to        // twice of front element        if (arr[i] >= 2 * now)            q.pop();         // Pushing each element of array        q.push(arr[i]);    }     return q.size();} // driver Programint main(){    int arr[] = { 4, 1, 2, 8 };    int n = sizeof(arr) / sizeof(arr[0]);    cout << minimumBox(arr, n) << endl;    return 0;}

## Java

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

## Python3

 # Python3 program to count number# of visible boxes. import collections # return the minimum number of visible boxesdef minimumBox(arr, n):     q = collections.deque([])     # sorting the array    arr.sort()     q.append(arr[0])     # traversing the array    for i in range(1, n):         now = q[0]         # checking if current element        # is greater than or equal to        # twice of front element        if(arr[i] >= 2 * now):            q.popleft()         # Pushing each element of array        q.append(arr[i])     return len(q) # driver Programif __name__=='__main__':    arr = [4, 1, 2, 8 ]    n = len(arr)    print(minimumBox(arr, n)) # This code is contributed by# Sanjit_Prasad

## C#

 // C# program to count number of visible// boxes.using System;using System.Collections.Generic; class GFG {     // return the minimum number of visible    // boxes    static int minimumBox(int []arr, int n)    {                 // New Queue of integers.        Queue q = new Queue();             // sorting the array        Array.Sort(arr);             q.Enqueue(arr[0]);                 // traversing the array        for (int i = 1; i < n; i++)        {            int now = q.Peek();                 // checking if current element            // is greater than or equal to            // twice of front element            if (arr[i] >= 2 * now)            q.Dequeue();                 // Pushing each element of array            q.Enqueue(arr[i]);        }             return q.Count;    }         // Driver code    public static void Main()    {        int [] arr = { 4, 1, 2, 8 };        int n = arr.Length;                 Console.WriteLine(minimumBox(arr, n));    }} // This code is contributed by Sam007.

## PHP

 = 2 * \$now)            array_pop(\$q);         // Pushing each element of array        array_push(\$q,\$arr[\$i]);    }     return count(\$q);} // Driver Code\$arr = array( 4, 1, 2, 8 );\$n = count(\$arr);echo minimumBox(\$arr, \$n); // This code is contributed by mits?>

## Javascript



Output:

1

Time Complexity: O(nlogn)

My Personal Notes arrow_drop_up