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.
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> |
Output
9
Time Complexity: O(1)
Auxiliary Space: O(1)