Count of smaller rectangles that can be placed inside a bigger rectangle
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 = 1
Output: 6
Explanation:
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
Output: 3
Explanation:
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++
#include <bits/stdc++.h>
using namespace std;
int No_of_rectangles( int L, int B,
int l, int b)
{
if ((l > L) || (b > B)) {
return -1;
}
else {
return (L - l + 1) * (B - b + 1);
}
}
int main()
{
int L = 5, B = 3;
int l = 4, b = 1;
cout << No_of_rectangles(L, B, l, b);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int No_of_rectangles( int L, int B,
int l, int b)
{
if ((l > L) || (b > B))
{
return - 1 ;
}
else
{
return (L - l + 1 ) * (B - b + 1 );
}
}
public static void main(String[] args)
{
int L = 5 , B = 3 ;
int l = 4 , b = 1 ;
System.out.println(No_of_rectangles(L, B, l, b));
}
}
|
Python3
def No_of_rectangles( L, B, l, b):
if (l > L) or (b > B):
return - 1 ;
else :
return (L - l + 1 ) * (B - b + 1 );
if __name__ = = '__main__' :
L = 5
B = 3
l = 4
b = 1
print (No_of_rectangles(L, B, l, b))
|
C#
using System;
class GFG{
static int No_of_rectangles( int L, int B,
int l, int b)
{
if ((l > L) || (b > B))
{
return -1;
}
else
{
return (L - l + 1) * (B - b + 1);
}
}
public static void Main(String[] args)
{
int L = 5, B = 3;
int l = 4, b = 1;
Console.Write(No_of_rectangles(L, B, l, b));
}
}
|
Javascript
<script>
function No_of_rectangles(L, B,
l, b)
{
if ((l > L) || (b > B))
{
return -1;
}
else
{
return (L - l + 1) * (B - b + 1);
}
}
let L = 5, B = 3;
let l = 4, b = 1;
document.write(No_of_rectangles(L, B, l, b));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
20 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...