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.
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;
void maxRectanglesPossible( int N)
{
if (N < 4 || N % 2 != 0) {
cout << -1 << "\n" ;
}
else
cout << (N / 2) - 1 << "\n" ;
}
int main()
{
int N = 20;
maxRectanglesPossible(N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static void maxRectanglesPossible( int N)
{
if (N < 4 || N % 2 != 0 ) {
System.out.println(- 1 );
}
else
System.out.println((N / 2 ) - 1 );
}
public static void main (String[] args) {
int N = 20 ;
maxRectanglesPossible(N);
}
}
|
Python3
def maxRectanglesPossible (N):
if (N < 4 or N % 2 ! = 0 ):
print ( "-1" );
else :
print ( int ((N / 2 ) - 1 ));
N = 20 ;
maxRectanglesPossible(N);
|
C#
using System;
class GFG {
static void maxRectanglesPossible( int N)
{
if (N < 4 || N % 2 != 0) {
Console.WriteLine(-1);
}
else
Console.WriteLine((N / 2) - 1);
}
public static void Main () {
int N = 20;
maxRectanglesPossible(N);
}
}
|
Javascript
<script>
const maxRectanglesPossible = (N) => {
if (N < 4 || N % 2 != 0) {
document.write( "-1<br/>" );
}
else
document.write(`${(N / 2) - 1}<br/>`);
}
let N = 20;
maxRectanglesPossible(N);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
09 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...