# C++ Program to find the Number of Visible Boxes After Putting One Inside Another

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

To solve a problem in which we are given an array containing the size of the boxes. Now we are given a condition that we can fit a smaller box inside a bigger box if the bigger box is at least twice the size of the smaller box. Now we must determine how many visible boxes there are, for example.

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

Input : arr[] = { 4, 2, 1, 8 }
Output : 1

## Approach to Find the Solution

In this problem, our approach will be to sort our array now as we move forward. We push elements to a queue now. We'll see if the current element is bigger than or equal to twice the queue's front element as we progress. If it is true, we pop the front element now. Finally, we push the current element in the queue. Our answer will be the size of our queue at the end.

## Example

#include <bits/stdc++.h>
using namespace std;
int main(){
int arr[] = { 1, 2, 3, 4, 5, 6 }; // given array containing the size of our boxes
int n = sizeof(arr) / sizeof(arr); // size of our array
queue<int> q;
sort(arr, arr + n); // sorting our array
q.push(arr); // pushing the smallest element in the front
for (int i = 1; i < n; i++) { // traversing the array
int curr = q.front(); // current element
if (arr[i] >= 2 * curr) // if the size of the current box is greater than twice
// of the box in front so we pop the front
q.pop();

q.push(arr[i]); // pushing the current element in the queue
}
cout << q.size() << "\n"; // our answer
return 0;
}

## Output

3

## Conclusion

In this tutorial, we solve a problem to find the Number of visible boxes after putting one inside another. We also learned the C++ program for this problem and the complete approach (Normal) by which we solved this problem. We can write the same program in other languages such as C, java, python, and other languages. We hope you find this tutorial helpful.