Given **width** and **height** of **N** rectangles. The task is to find the minimum number of rectangles left after inserting one into another.

**Note :**

- If W1 < W2 and H1 < H2 then rectangle 1 fits inside rectangle 2.
- The smallest rectangle can insert in the second smallest, and this rectangle can insert in the next one and so forth.

**Examples:**

Input :arr[] = {{20, 30}, {10, 10}, {30, 20}, {40, 50}};Output :2Explanation :One of the possible way is to insert second recatngle in first and then insert first rectangle in fourth. Then finally, third and fourth rectangles left.Input :arr[] = {{10, 30}, {20, 20}, {30, 10}};Output :3Explanation :Can't place any rectangle in any other one. So, three rectangles left.

**Approach** :

- Firstly sort all rectangles such that the heights are in decreasing order. We will first assume that each height is unique (later we will extend our approach to the case where there are the same heights).
- We maintain another array nested[i]. Starting from the tallest rectangle down to the shortest, we will try to fit in the rectangle to nested[i], by finding a nested rectangle in nested[i] in which its width is bigger than that of our current rectangle. Not only that, we want to place it in the one with the minimum width. We then place the rectangle inside that nested rectangle and update its new height and weight. Why the minimum one? Because if we place the rectangle on another one with a width bigger than that of minimum width, we can apply the following exchange argument:

Let’s assume there exist an optimal arrangement such that the current rectangle[i] is not placed on nested[m] of minimum width that satisfies the above requirement. Suppose rectangle[i] is placed on nested[n], and another rectangle[j] is placed on nested[m] instead. Then since rectangle[j] can fit in nested[n], it can also fit in nested[m], and hence we can swap rectangle[i] and rectangle[j]. By performing the exchange to all such rectangles at all stage, we can transform this optimal arrangement to our greedy arrangement. Hence our greedy arrangement is also optimal. - Inductively, we can prove that the rectangles in nested[i] are always sorted in increasing width.
- Lastly, in the case that there is a rectangle with the same heights, we sort the widths in increasing order so as to maintain the sorting order of nested[i].

Below is the implementation of the above approach:

`// CPP program to find the minimum number of rectangles ` `// left after inserting one into another ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function for comparison ` `bool` `comp(` `const` `pair<` `int` `, ` `int` `>& L, ` `const` `pair<` `int` `, ` `int` `>& R) ` `{ ` ` ` `if` `(L.first == R.first) ` ` ` `return` `L.second > R.second; ` ` ` ` ` `return` `L.first < R.first; ` `} ` ` ` `// Function to find the minimum number of rectangles ` `// left after inserting one into another ` `int` `Rectangles(pair<` `int` `, ` `int` `> rectangle[], ` `int` `n) ` `{ ` ` ` `// Sort rectangles in increasing order of width ` ` ` `// and decreasing order of height ` ` ` `sort(rectangle, rectangle + n, comp); ` ` ` ` ` `vector<pair<` `int` `, ` `int` `> > nested; ` ` ` ` ` `// Keep the largest rectangle ` ` ` `nested.push_back(rectangle[n - 1]); ` ` ` ` ` `// For all remaining rectangles ` ` ` `for` `(` `int` `i = n - 2; i >= 0; --i) { ` ` ` `int` `high = nested.size() - 1, low = 0; ` ` ` ` ` `// Fidn the position of this rectangle in nested ` ` ` `while` `(low <= high) { ` ` ` `int` `mid = (high + low) / 2; ` ` ` `if` `(nested[mid].first == rectangle[i].first ` ` ` `|| nested[mid].second <= rectangle[i].second) ` ` ` `low = mid + 1; ` ` ` ` ` `else` ` ` `high = mid - 1; ` ` ` `} ` ` ` ` ` `// If this rectangle not possible to insert in ` ` ` `// any other rectangle ` ` ` `if` `(low == nested.size()) ` ` ` `nested.push_back(rectangle[i]); ` ` ` ` ` `// Replace with previous rectangle ` ` ` `else` `{ ` ` ` `nested[low].second = rectangle[i].second; ` ` ` `nested[low].first = rectangle[i].first; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `((` `int` `)nested.size()); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// list of Width, Height pair ` ` ` `pair<` `int` `, ` `int` `> arr[] = { { 20, 30 }, { 10, 10 }, { 30, 20 }, { 40, 50 } }; ` ` ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout << Rectangles(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Number of visible boxes after putting one inside another
- Print array of strings in sorted order without copying one string into another
- Sum of all second largest divisors after splitting a number into one or more parts
- Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m
- Minimum element left from the array after performing given operations
- Count of strings that become equal to one of the two strings after one removal
- Minimum area of square holding two identical rectangles
- Minimize the sum calculated by repeatedly removing any two elements and inserting their sum to the Array
- Make sum of all subarrays of length K equal by only inserting elements
- Count of Distinct strings possible by inserting K characters in the original string
- Find the index of the left pointer after possible moves in the array
- Find the Mth element of the Array after K left rotations
- Find if two rectangles overlap
- Find side of Square which makes minimal area to fit two identical rectangles inside it
- Number of chocolates left after k iterations
- Number of unique rectangles formed using N unit squares
- Number of rectangles in N*M grid
- Total number of unit cells covered by all given Rectangles
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Minimize the maximum minimum difference after one removal from array

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.