Open In App

Count of maximum distinct Rectangles possible with given Perimeter

Last Updated : 09 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
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 = 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)



Similar Reads

Perimeter of the Union of Two Rectangles
Given two arrays X[] and Y[], each of length 4, where (X[0], Y[0]) and (X[1], Y[1]) represents the bottom left and top right corners of one rectangle and (X[2], Y[2]) and (X[3], Y[3]) represents the bottom left and top right corners of the other rectangle, the task is to find the perimeter of the outer boundaries of the union of the two rectangles
7 min read
Count pairs of similar rectangles possible from a given array
Given a 2D array A[][2] of size N (1 ? N ? 103), where A[i][0] and A[i][1] denotes the length and breadth of rectangle i respectively. Two rectangle i and j where (i &lt; j) are similar if the ratio of their length and breadth is equal A[i][0] / A[i][1] = A[j][0] / A[j][1] The task is to count the pair of rectangles that are nearly similar. Example
9 min read
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: For area to be maximum of any rectangle the diffe
3 min read
Count of distinct rectangles inscribed in an equilateral triangle
Given an equilateral triangle made of dots (.) joined together to form triangle and an integer n which represents the length of the sides of the triangle. The task is to count the number of rectangles that can be inscribed in the given triangle such that: Horizontal edges must be parallel to the base of the given triangle.Rectangle must only be for
11 min read
Count Distinct Rectangles in N*N Chessboard
Given a N x N Chessboard. The task is to count distinct rectangles from the chessboard. For example, if the input is 8 then the output should be 36.Examples: Input: N = 4 Output: 10 Input: N = 6 Output: 21 Approach: Suppose N = 8 i.e. 8 x 8 chessboard is given, So different rectangles that can be formed are: 1 x 1, 1 x 2, 1 x 3, 1 x 4, 1 x 5, 1 x 6
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 triangles = 8 So the aim is to find the number of s
6 min read
Count of rectangles possible from N and M straight lines parallel to X and Y axis respectively
Given two integers N and M, where N straight lines are parallel to the X-axis and M straight lines are parallel to Y-axis, the task is to calculate the number of rectangles that can be formed by these lines. Examples: Input: N = 3, M = 6 Output: 45 Explanation: There are total 45 rectangles possible with 3 lines parallel to x axis and 6 lines paral
5 min read
Find vertex coordinates of all possible rectangles with a given vertex and dimensions
Given two integers L and B representing the length and breadth of a rectangle and a coordinate (X, Y) representing a point on the cartesian plane, the task is to find coordinates of all rectangles having a vertex as (X, Y) of the given dimensions. Example: Input: X=9, Y=9, L=5, B=3Output:(9, 9), (14, 9), (9, 12), (14, 12)(4, 9), (9, 9), (4, 12), (9
8 min read
Count rectangles generated in a given rectangle by lines drawn parallel to X and Y axis from a given set of points
Given a 2D array rectangle[][] representing vertices of a rectangle {(0, 0), (L, 0), (0, B), (L, B)} of dimensions L * B, and another 2D-array, points[][] of size N in a Cartesian coordinate system. Draw a horizontal line parallel to the X-axis and a vertical line parallel to the Y-axis from each point of the given array. The task is to count all p
6 min read
Sum of Areas of Rectangles possible for an array
Given an array, the task is to compute the sum of all possible maximum area rectangles which can be formed from the array elements. Also, you can reduce the elements of the array by at most 1. Examples: Input: a = {10, 10, 10, 10, 11, 10, 11, 10} Output: 210 Explanation: We can form two rectangles one square (10 * 10) and one (11 * 10). Hence, tota
13 min read