Skip to content
Related Articles

Related Articles

Improve Article

Find the minimum number of rectangles left after inserting one into another

  • Difficulty Level : Hard
  • Last Updated : 08 Sep, 2021
Geek Week

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

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


Input : arr[] = {{20, 30}, {10, 10}, {30, 20}, {40, 50}};
Output : 2
Explanation : One of the possible way is to insert 
second rectangle 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 : 3
Explanation : Can't place any rectangle in any other one. 
So, three rectangles left.



  1. 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).
  2. 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.
  3. Inductively, we can prove that the rectangles in nested[i] are always sorted in increasing width.
  4. 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;
        // Find 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;
                high = mid - 1;
        // If this rectangle not possible to insert in
        // any other rectangle
        if (low == nested.size())
        // 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;


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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up
Recommended Articles
Page :