VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-of-maximum-distinct-rectangles-possible-with-given-perimeter/

⇱ Count of maximum distinct Rectangles possible with given Perimeter - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count of maximum distinct Rectangles possible with given Perimeter

Last Updated : 9 Feb, 2022

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. 

Examples

Input: N = 10
Output: 4
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
Output: 3

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.


Output
9

Time Complexity: O(1) 
Auxiliary Space: O(1)

Comment