Maximum area of rectangle possible with given perimeter

30 May, 2021
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 difference of length and breadth must be minimal. So, in such case the length must be ceil (perimeter / 4) and breadth will be be floor(perimeter /4). Hence the maximum area of a rectangle with given perimeter is equal to ceil(perimeter/4) * floor(perimeter/4).
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ to find maximum area rectangle#include <bits/stdc++.h>using namespace std; // Function to find max areaint maxArea(float perimeter){    int length = (int)ceil(perimeter / 4);    int breadth = (int)floor(perimeter / 4);     // return area    return length * breadth;} // Driver codeint main(){    float n = 38;    cout << "Maximum Area = " << maxArea(n);     return 0;}
//Java to find maximum area rectangle import java.io.*; class GFG {// Function to find max areastatic int maxArea(float perimeter){    int length = (int)Math.ceil(perimeter / 4);    int breadth = (int)Math.floor(perimeter / 4); // return areareturn length * breadth;} // Driver code         public static void main (String[] args) {         float n = 38;        System.out.println("Maximum Area = " +                maxArea(n));             }}
# Python3 program to find# maximum area rectanglefrom math import ceil, floor # Function to find max areadef maxArea(perimeter):    length = int(ceil(perimeter / 4))    breadth = int(floor(perimeter / 4))     # return area    return length * breadth # Driver codeif __name__ == '__main__':    n = 38    print("Maximum Area =", maxArea(n))
// C# to find maximum area rectangleusing System; class GFG{// Function to find max areastatic int maxArea(float perimeter){    int length = (int)Math.Ceiling(perimeter / 4);    int breadth = (int)Math.Floor(perimeter / 4);     // return area    return length * breadth;} // Driver codepublic static void Main(){    float n = 38;    Console.WriteLine("Maximum Area = " +                             maxArea(n));}} // This code is contributed// by Akanksha Rai(Abby_akku)
<?php// PHP to find maximum area rectangle // Function to find max areafunction maxArea($perimeter){    $length = (int)ceil($perimeter / 4);    $breadth = (int)floor($perimeter / 4);     // return area    return ($length * $breadth);} // Driver code$n = 38;echo "Maximum Area = " , maxArea($n); // This code is contributed by jit_t?>
<script> // JavaScript to find maximum area rectangle // Function to find max areafunction maxArea(perimeter){    let length = Math.ceil(perimeter / 4);    let breadth = Math.floor(perimeter / 4);     // return area    return length * breadth;} // Driver codelet n = 38; document.write("Maximum Area = " + maxArea(n)); // This code is contributed by Manoj. </script>
Maximum Area = 90
 
Time Complexity: O(1)Auxiliary Space: O(1)
SURENDRA_GANGWAR
jit_t
Akanksha_Rai
mank1083
ujjwalgoel1103
area-volume-programs
Geometric
Mathematical
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum Manhattan distance between a distinct pair from N coordinates
Total area of two overlapping rectangles
Largest area possible after removal of a series of horizontal & vertical bars
Program to find line passing through 2 Points
Equation of circle when three points on the circle are given
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Merge two sorted arrays