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 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.
Implementation:
C++
// 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; } |
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<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. |
Python3
# 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 |
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< 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. |
PHP
<?php // PHP program to count number of visible boxes. // return the minimum number of visible boxes function minimumBox( $arr , $n ) { $q = array (); // sorting the array sort( $arr ); array_push ( $q , $arr [0]); // traversing the array for ( $i = 1; $i < $n ; $i ++) { $now = $q [0]; // checking if current element // is greater than or equal to // twice of front element if ( $arr [ $i ] >= 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
<script> // Javascript program to count // number of visible boxes. // return the minimum number // of visible boxes function minimumBox(arr, n) { var q = []; // sorting the array arr.sort((a,b)=> a-b) q.push(arr[0]); // traversing the array for ( var i = 1; i < n; i++) { var now = q[0]; // checking if current element // is greater than or equal to // twice of front element if (arr[i] >= 2 * now) q.pop(0); // Pushing each element of array q.push(arr[i]); } return q.length; } // driver Program var arr = [ 4, 1, 2, 8 ]; var n = arr.length; document.write( minimumBox(arr, n)); </script> |
1
Time Complexity: O(nlogn)
Auxiliary Space: O(n) for queue
Efficient approach: Using Stack
We can also solve this problem using a stack.
- We start with an empty stack and push the first element of the array onto the stack.
- for each subsequent element of the array, we compare it with the top element of the stack.
- If the current element is greater than or equal to twice the top element, we pop the stack until the current element is less than twice the top element or the stack becomes empty.
- Finally, we push the current element onto the stack. The size of the stack at the end gives us the minimum number of visible boxes.
Implementation :
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // This function takes an array of integers and the size of the array as input int minimumBox( int arr[], int n) { // Create a stack to store the maximum heights of the visible boxes stack< int > st; // Push the first element of the array onto the stack st.push(arr[0]); // Traverse the remaining elements of the array for ( int i = 1; i < n; i++) { // Keep popping elements from the stack while they are smaller than // or equal to twice the current element, since they cannot be visible while (!st.empty() && arr[i] >= 2 * st.top()) st.pop(); // Push the current element onto the stack st.push(arr[i]); } // The size of the stack is the minimum number of visible boxes return st.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; } // this code is contributed by bhardwajji |
Java
import java.util.Stack; public class Main { // This function takes an array of integers and the size of the array as input public static int minimumBox( int [] arr, int n) { // Create a stack to store the maximum heights of the visible boxes Stack<Integer> st = new Stack<>(); // Push the first element of the array onto the stack st.push(arr[ 0 ]); // Traverse the remaining elements of the array for ( int i = 1 ; i < n; i++) { // Keep popping elements from the stack while they are smaller than // or equal to twice the current element, since they cannot be visible while (!st.empty() && arr[i] >= 2 * st.peek()) st.pop(); // Push the current element onto the stack st.push(arr[i]); } // The size of the stack is the minimum number of visible boxes return st.size(); } // driver Program 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 shivhack999 |
Python3
# This function takes an array of integers and the size of the array as input def minimum_box(arr, n): # Create a stack to store the maximum heights of the visible boxes st = [] # Push the first element of the array onto the stack st.append(arr[ 0 ]) # Traverse the remaining elements of the array for i in range ( 1 , n): # Keep popping elements from the stack while they are smaller than # or equal to twice the current element, since they cannot be visible while len (st) ! = 0 and arr[i] > = 2 * st[ - 1 ]: st.pop() # Push the current element onto the stack st.append(arr[i]) # The size of the stack is the minimum number of visible boxes return len (st) # driver Program arr = [ 4 , 1 , 2 , 8 ] n = len (arr) print (minimum_box(arr, n)) |
Javascript
// This function takes an array of integers and the size of the array as input function minimumBox(arr, n) { // Create a stack to store the maximum heights of the visible boxes const st = []; // Push the first element of the array onto the stack st.push(arr[0]); // Traverse the remaining elements of the array for (let i = 1; i < n; i++) { // Keep popping elements from the stack while they are smaller than // or equal to twice the current element, since they cannot be visible while (st.length !== 0 && arr[i] >= 2 * st[st.length - 1]) { st.pop(); } // Push the current element onto the stack st.push(arr[i]); } // The size of the stack is the minimum number of visible boxes return st.length; } // Driver program const arr = [4, 1, 2, 8]; const n = arr.length; console.log(minimumBox(arr, n)); |
C#
using System; using System.Collections.Generic; public class Program { public static int MinimumBox( int [] arr, int n) { Stack< int > st = new Stack< int >(); st.Push(arr[0]); for ( int i = 1; i < n; i++) { while (st.Count != 0 && arr[i] >= 2 * st.Peek()) { st.Pop(); } st.Push(arr[i]); } return st.Count; } public static void Main() { int [] arr = new int [] { 4, 1, 2, 8 }; int n = arr.Length; Console.WriteLine(MinimumBox(arr, n)); // Output: 2 } } |
1
Time Complexity: O(n)
Auxiliary Space: O(n) for stack
Please Login to comment...