- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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[0]); // size of our array queue<int> q; sort(arr, arr + n); // sorting our array q.push(arr[0]); // 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.

- Related Questions & Answers
- Program to find maximum number of boxes we can fit inside another boxes in python
- Find the minimum number of rectangles left after inserting one into another in C++
- Program to find minimum number of operations required to make one number to another in Python
- Find the number of boxes to be removed in C++
- Program to find longest number of 1s after swapping one pair of bits in Python
- Program to find number of steps required to change one word to another in Python
- Program to find number of boxes that form longest chain in Python?
- Program to find out the number of boxes to be put into the godown in Python
- Program to find number of sublists containing maximum and minimum after deleting only one element in Python
- Maximum segment value after putting k breakpoints in a number in C++
- Program to find longest subarray of 1s after deleting one element using Python
- Find the minimum number of moves needed to move from one cell of matrix to another in Python
- Program to find sum of digits that are inside one alphanumeric string in Python
- Program to check all 1s are present one after another or not in Python
- Find the Number of Ways to go From One Point to Another in a Grid using C++