Count of maximum distinct Rectangles possible with given Perimeter Last Updated : 09 Feb, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 = 10Output: 4Explanation: All the rectangles with perimeter 10 are following in the form of (length, breadth):(1, 4), (4, 1), (2, 3), (3, 2) Input: N = 8Output: 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. C++ #include <iostream> using namespace std; // Function to find the maximum number // of distinct rectangles with given perimeter void maxRectanglesPossible(int N) { // Invalid case if (N < 4 || N % 2 != 0) { cout << -1 << "\n"; } else // Number of distinct rectangles. cout << (N / 2) - 1 << "\n"; } // Driver Code int main() { // Perimeter of the rectangle. int N = 20; maxRectanglesPossible(N); return 0; } Java // Java program for the above approach import java.io.*; import java.lang.*; import java.util.*; class GFG { // Function to find the maximum number // of distinct rectangles with given perimeter static void maxRectanglesPossible(int N) { // Invalid case if (N < 4 || N % 2 != 0) { System.out.println(-1); } else // Number of distinct rectangles. System.out.println((N / 2) - 1); } // Driver Code public static void main (String[] args) { // Perimeter of the rectangle. int N = 20; maxRectanglesPossible(N); } } // This code is contributed by hrithikgarg0388. Python3 # Function to find the maximum number # of distinct rectangles with given perimeter def maxRectanglesPossible (N): # Invalid case if (N < 4 or N % 2 != 0): print("-1"); else: # Number of distinct rectangles. print(int((N / 2) - 1)); # Driver Code # Perimeter of the rectangle. N = 20; maxRectanglesPossible(N); # This code is contributed by gfgking C# // C# program for the above approach using System; class GFG { // Function to find the maximum number // of distinct rectangles with given perimeter static void maxRectanglesPossible(int N) { // Invalid case if (N < 4 || N % 2 != 0) { Console.WriteLine(-1); } else // Number of distinct rectangles. Console.WriteLine((N / 2) - 1); } // Driver Code public static void Main () { // Perimeter of the rectangle. int N = 20; maxRectanglesPossible(N); } } // This code is contributed by Samim Hossain Mondal. JavaScript <script> // Function to find the maximum number // of distinct rectangles with given perimeter const maxRectanglesPossible = (N) => { // Invalid case if (N < 4 || N % 2 != 0) { document.write("-1<br/>"); } else // Number of distinct rectangles. document.write(`${(N / 2) - 1}<br/>`); } // Driver Code // Perimeter of the rectangle. let N = 20; maxRectanglesPossible(N); // This code is contributed by rakeshsahni </script> Output9 Time Complexity: O(1) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Count of maximum distinct Rectangles possible with given Perimeter P priyanshusingh241202 Follow Improve Article Tags : Misc Mathematical Geometric DSA square-rectangle +1 More Practice Tags : GeometricMathematicalMisc Similar Reads Maximum area of rectangle possible with given perimeter Given the perimeter of a rectangle, the task is to find the maximum area of a rectangle which can use n-unit length as its perimeter. Note: Length and Breadth must be an integral value. Example: Input: perimeter = 15 Output: Maximum Area = 12 Input: perimeter = 16 Output: Maximum Area = 16 Approach: 3 min read Count number of right triangles possible with a given perimeter Given a perimeter P, the task is to find the number of right triangles possible with perimeter equal to p.Examples: Input: P = 12 Output: number of right triangles = 1 The only right angle possible is with sides hypotenuse = 5, perpendicular = 4 and base = 3. Input: p = 840 Output: number of right t 6 min read Find minimum area of rectangle with given set of coordinates Given an array arr of set of points in the X-Y plane. The task is to find the minimum area of a rectangle that can be formed from these points. The sides of the rectangle should be parallel to the X and Y axes. If a rectangle cannot be formed with the given points then print 0 .Examples: Input: arr[ 5 min read Maximum given sized rectangles that can be cut out of a sheet of paper Given the length L and breadth B of a sheet of paper, the task is to find the maximum number of rectangles with given length l and breadth b that can be cut from this sheet of paper.Examples: Input: L = 5, B = 2, l = 14, b = 3 Output: 0 The sheet is smaller than the required rectangle. So, no rectan 6 min read Maximum Squares possible parallel to both axes from N distinct points Given Xi and Yi co-ordinate of N distinct points in the 2-D Plane, the task is to count the number of squares that can be formed from these points that are parallel to both X and Y-axis.Examples: Input : X[] = { 0, 2, 0, 2 }, Y[] = { 0, 2, 2, 0 } Output : 1 Explanation: Only one Square can be formed 9 min read Like