Given four integers L, B, l, and b, where L and B denote the dimensions of a bigger rectangle and l and b denotes the dimension of a smaller rectangle, the task is to count the number of smaller rectangles that can be drawn inside a bigger rectangle.
Note: Smaller rectangles can overlap partially.
Input: L = 5, B = 3, l = 4, b = 1
There are 6 rectangles of dimension 4 × 1 that can be drawn inside a bigger rectangle of dimension 5 × 3.
Input: L = 3, B = 2, l = 2, b = 1
There are 3 rectangles of dimension 3 × 2 can be drawn inside a bigger rectangle of dimension 2 × 1.
Naive Approach: The idea is to iterate over the length L and breadth B of the bigger rectangle to count the number of smaller rectangles of dimension l x b that can be drawn within the range of bigger rectangle. Print the total count after the traversal.
Time Complexity: O(L * B)
Auxiliary Space: O(1)
Efficient Approach: The above problem can be solved using Permutation and Combinations. Below are the steps:
- The total possible values of the length of smaller rectangle l using the length L is given by (L – l + 1).
- The total possible values of the breadth of smaller rectangle b using the length B is given by (B – b + 1).
- Hence, the total number of possible rectangles can be formed is given by:
(L – l + 1) * (B – b + 1)
Below is the implementation of the above approach:
Time Complexity: O(1)
Auxiliary Space: O(1)
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.
- Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m
- Largest subset of rectangles such that no rectangle fit in any other rectangle
- Count rectangles generated in a given rectangle by lines drawn parallel to X and Y axis from a given set of points
- Maximum area of a Rectangle that can be circumscribed about a given Rectangle of size LxW
- Intersecting rectangle when bottom-left and top-right corners of two rectangles are given
- Find side of Square which makes minimal area to fit two identical rectangles inside it
- Count the number of rhombi possible inside a rectangle of given size
- Maximum non-attacking Rooks that can be placed on an N*N Chessboard
- Check if all objects of type A and B can be placed on N shelves
- Ratio of area of a rectangle with the rectangle inscribed in it
- Check whether a given point lies inside a rectangle or not
- Coordinates of rectangle with given points lie inside
- Check if a point lies on or inside a rectangle | Set-2
- Check whether a given point lies on or inside the rectangle | Set 3
- Sum of Area of all possible square inside a rectangle
- Generate all integral points lying inside a rectangle
- Maximum given sized rectangles that can be cut out of a sheet of paper
- Check if N rectangles of equal area can be formed from (4 * N) integers
- Sum of the products of same placed digits of two numbers
- Count of distinct rectangles inscribed in an equilateral triangle
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Improved By : jana_sayantan