Count of maximum distinct Rectangles possible with given Perimeter
Given an integer N denoting the perimeter of a rectangle. The task is to find the number of distinct rectangles possible with a given perimeter.
Input: N = 10
Explanation: All the rectangles with perimeter 10 are following in the form of (length, breadth):
(1, 4), (4, 1), (2, 3), (3, 2)
Input: N = 8
Approach: This problem can be solved by using the properties of rectangles. Follow the steps below to solve the given problem.
- The perimeter of a rectangle is 2*(length + breadth).
- If N is odd, then there is no rectangle possible. As perimeter can never be odd.
- If N is less than 4 then also, there cannot be any rectangle possible. As the minimum possible length of a side is 1, even if the length of all the sides is 1 then also the perimeter will be 4.
- Now N = 2*(l + b) and (l + b) = N/2.
- So, it is required to find all the pairs whose sum is N/2 which is (N/2) – 1.
Below is the implementation of the above approach.
Time Complexity: O(1)
Auxiliary Space: O(1)