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.

**Examples:**

Input:L = 5, B = 3, l = 4, b = 1Output:6Explanation:

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 = 1Output:3Explanation:

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:

## C++

`// C++ program for the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count smaller rectangles ` `// within the larger rectangle ` `int` `No_of_rectangles(` `int` `L, ` `int` `B, ` ` ` `int` `l, ` `int` `b) ` `{ ` ` ` `// If the dimension of the smaller ` ` ` `// rectangle is greater than the ` ` ` `// bigger one ` ` ` `if` `((l > L) || (b > B)) { ` ` ` `return` `-1; ` ` ` `} ` ` ` ` ` `else` `{ ` ` ` ` ` `// Return the number of smaller ` ` ` `// rectangles possible ` ` ` `return` `(L - l + 1) * (B - b + 1); ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Dimension of bigger rectangle ` ` ` `int` `L = 5, B = 3; ` ` ` ` ` `// Dimension of smaller rectangle ` ` ` `int` `l = 4, b = 1; ` ` ` ` ` `// Function call ` ` ` `cout << No_of_rectangles(L, B, l, b); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to count smaller rectangles ` `// within the larger rectangle ` `static` `int` `No_of_rectangles(` `int` `L, ` `int` `B, ` ` ` `int` `l, ` `int` `b) ` `{ ` ` ` ` ` `// If the dimension of the smaller ` ` ` `// rectangle is greater than the ` ` ` `// bigger one ` ` ` `if` `((l > L) || (b > B)) ` ` ` `{ ` ` ` `return` `-` `1` `; ` ` ` `} ` ` ` ` ` `else` ` ` `{ ` ` ` ` ` `// Return the number of smaller ` ` ` `// rectangles possible ` ` ` `return` `(L - l + ` `1` `) * (B - b + ` `1` `); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` ` ` `// Dimension of bigger rectangle ` ` ` `int` `L = ` `5` `, B = ` `3` `; ` ` ` ` ` `// Dimension of smaller rectangle ` ` ` `int` `l = ` `4` `, b = ` `1` `; ` ` ` ` ` `// Function call ` ` ` `System.out.println(No_of_rectangles(L, B, l, b)); ` `} ` `} ` ` ` `// This code is contributed by jana_sayantan ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` ` ` `# Function to count smaller rectangles ` `# within the larger rectangle ` `def` `No_of_rectangles( L, B, l, b): ` ` ` ` ` `# If the dimension of the smaller ` ` ` `# rectangle is greater than the ` ` ` `# bigger one ` ` ` `if` `(l > L) ` `or` `(b > B): ` ` ` `return` `-` `1` `; ` ` ` ` ` `else` `: ` ` ` ` ` `# Return the number of smaller ` ` ` `# rectangles possible ` ` ` `return` `(L ` `-` `l ` `+` `1` `) ` `*` `(B ` `-` `b ` `+` `1` `); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `# Dimension of bigger rectangle ` ` ` `L ` `=` `5` ` ` `B ` `=` `3` ` ` ` ` `# Dimension of smaller rectangle ` ` ` `l ` `=` `4` ` ` `b ` `=` `1` ` ` ` ` `# Function call ` ` ` `print` `(No_of_rectangles(L, B, l, b)) ` ` ` `# This code is contributed by jana_sayantan ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to count smaller rectangles ` `// within the larger rectangle ` `static` `int` `No_of_rectangles(` `int` `L, ` `int` `B, ` ` ` `int` `l, ` `int` `b) ` `{ ` ` ` ` ` `// If the dimension of the smaller ` ` ` `// rectangle is greater than the ` ` ` `// bigger one ` ` ` `if` `((l > L) || (b > B)) ` ` ` `{ ` ` ` `return` `-1; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` ` ` `// Return the number of smaller ` ` ` `// rectangles possible ` ` ` `return` `(L - l + 1) * (B - b + 1); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` ` ` `// Dimension of bigger rectangle ` ` ` `int` `L = 5, B = 3; ` ` ` ` ` `// Dimension of smaller rectangle ` ` ` `int` `l = 4, b = 1; ` ` ` ` ` `// Function call ` ` ` `Console.Write(No_of_rectangles(L, B, l, b)); ` `} ` `} ` ` ` `// This code is contributed by jana_sayantan ` |

*chevron_right*

*filter_none*

**Output:**

6

**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.

## Recommended Posts:

- 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 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.