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++
#include <bits/stdc++.h>
using namespace std;
int minimumBox( int arr[], int n)
{
queue< int > q;
sort(arr, arr + n);
q.push(arr[0]);
for ( int i = 1; i < n; i++) {
int now = q.front();
if (arr[i] >= 2 * now)
q.pop();
q.push(arr[i]);
}
return q.size();
}
int main()
{
int arr[] = { 4, 1, 2, 8 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << minimumBox(arr, n) << endl;
return 0;
}
|
Java
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));
}
}
|
Python3
import collections
def minimumBox(arr, n):
q = collections.deque([])
arr.sort()
q.append(arr[ 0 ])
for i in range ( 1 , n):
now = q[ 0 ]
if (arr[i] > = 2 * now):
q.popleft()
q.append(arr[i])
return len (q)
if __name__ = = '__main__' :
arr = [ 4 , 1 , 2 , 8 ]
n = len (arr)
print (minimumBox(arr, n))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int minimumBox( int []arr, int n)
{
Queue< int > q = new Queue< int >();
Array.Sort(arr);
q.Enqueue(arr[0]);
for ( int i = 1; i < n; i++)
{
int now = q.Peek();
if (arr[i] >= 2 * now)
q.Dequeue();
q.Enqueue(arr[i]);
}
return q.Count;
}
public static void Main()
{
int [] arr = { 4, 1, 2, 8 };
int n = arr.Length;
Console.WriteLine(minimumBox(arr, n));
}
}
|
PHP
<?php
function minimumBox( $arr , $n )
{
$q = array ();
sort( $arr );
array_push ( $q , $arr [0]);
for ( $i = 1; $i < $n ; $i ++)
{
$now = $q [0];
if ( $arr [ $i ] >= 2 * $now )
array_pop ( $q );
array_push ( $q , $arr [ $i ]);
}
return count ( $q );
}
$arr = array ( 4, 1, 2, 8 );
$n = count ( $arr );
echo minimumBox( $arr , $n );
?>
|
Javascript
<script>
function minimumBox(arr, n)
{
var q = [];
arr.sort((a,b)=> a-b)
q.push(arr[0]);
for ( var i = 1; i < n; i++) {
var now = q[0];
if (arr[i] >= 2 * now)
q.pop(0);
q.push(arr[i]);
}
return q.length;
}
var arr = [ 4, 1, 2, 8 ];
var n = arr.length;
document.write( minimumBox(arr, n));
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
int minimumBox( int arr[], int n)
{
stack< int > st;
st.push(arr[0]);
for ( int i = 1; i < n; i++) {
while (!st.empty() && arr[i] >= 2 * st.top())
st.pop();
st.push(arr[i]);
}
return st.size();
}
int main()
{
int arr[] = { 4, 1, 2, 8 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << minimumBox(arr, n) << endl;
return 0;
}
|
Java
import java.util.Stack;
public class Main {
public static int minimumBox( int [] arr, int n) {
Stack<Integer> st = new Stack<>();
st.push(arr[ 0 ]);
for ( int i = 1 ; i < n; i++) {
while (!st.empty() && arr[i] >= 2 * st.peek())
st.pop();
st.push(arr[i]);
}
return st.size();
}
public static void main(String[] args) {
int [] arr = { 4 , 1 , 2 , 8 };
int n = arr.length;
System.out.println(minimumBox(arr, n));
}
}
|
Python3
def minimum_box(arr, n):
st = []
st.append(arr[ 0 ])
for i in range ( 1 , n):
while len (st) ! = 0 and arr[i] > = 2 * st[ - 1 ]:
st.pop()
st.append(arr[i])
return len (st)
arr = [ 4 , 1 , 2 , 8 ]
n = len (arr)
print (minimum_box(arr, n))
|
Javascript
function minimumBox(arr, n) {
const st = [];
st.push(arr[0]);
for (let i = 1; i < n; i++) {
while (st.length !== 0 && arr[i] >= 2 * st[st.length - 1]) {
st.pop();
}
st.push(arr[i]);
}
return st.length;
}
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));
}
}
|
Time Complexity: O(n)
Auxiliary Space: O(n) for stack
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!