Number of visible boxes after putting one inside another

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 trying 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to count number of visible boxes.
#include <bits/stdc++.h>
using namespace std;
  
// return the minimum number of visible boxes
int minimumBox(int arr[], int n)
{
    queue<int> 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 Program
int main()
{
    int arr[] = { 4, 1, 2, 8 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << minimumBox(arr, n) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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<Integer> 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.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to count number 
# of visible boxes.
  
import collections
  
# return the minimum number of visible boxes
def 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 Program
if __name__=='__main__':
    arr = [4, 1, 2, 8 ]
    n = len(arr)
    print(minimumBox(arr, n))
  
# This code is contributed by
# Sanjit_Prasad

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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<int> q = new Queue<int>();
      
        // 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.

chevron_right


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
?>


Output –

1

Time Complexity: O(nlogn)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.