Area of the largest Rectangle without a given point
Last Updated :
05 May, 2021
Given the length L and breadth B of a rectangle and the position of a hole in the rectangle as (X, Y) coordinate, the task is to find the area of largest Rectangle within the given Rectangle such that it does not contain the hole.
Note: The rectangle is placed at the origin by two of its side touching the Co-ordinate axis.
Examples:
Input: L = 8, B = 8, X = 0, Y = 0
Output: 56
Explanation:
Since the hole is at origin, i.e. (0, 0), the maximum area rectangle can be cut from either (0, 1) or (1, 0) by reducing the length or breadth of the rectangle by one.
Hence, the maximum area rectangle that can be formed is = 7 * 8 = 56
Input: L = 1, B = 10, X = 0, Y = 3
Output: 6
Explanation:
Since the hole is at (0, 3), the maximum area rectangle can be cutted from the point (0, 4) by reducing the breadth to 6 and keeping the length as 1.
Hence, the maximum area rectangle that can be formed is = 6 * 1 = 6
Approach: In order to avoid the hole, the rectangle can be cut from either above, below, left or right of the hole, as:
Position - Maximum area of rectangle
------------------------------------
Left - X * B
Right - (L - X - 1) * B
Above - L * Y
Below - (B - Y - 1) * L
Therefore, the required area of the largest rectangle can be computed by comparing the area calculated by using the above positions. The position with the largest area will yield the result.
Below is the implementation of the above approach:
C++
// C++ implementation to find area of
// largest Rectangle without hole
// within a given Rectangle
#include <bits/stdc++.h>
using namespace std;
// Function to find the maximum area
// such that it does not contains any hole
void maximumArea(int l, int b,
int x, int y)
{
// Area for all the possible
// positions of the cut
int left, right, above, below;
left = x * b;
right = (l - x - 1) * b;
above = l * y;
below = (b - y - 1) * l;
// Find the maximum area
// among the above rectangles
cout << max(max(left, right),
max(above, below));
}
// Driver Code
int main()
{
int L = 8, B = 8;
int X = 0, Y = 0;
// Function call
maximumArea(l, b, x, y);
return 0;
}
Java
// Java implementation to find area of
// largest Rectangle without hole
// within a given Rectangle
import java.util.*;
class GFG{
// Function to find the maximum area
// such that it does not contains any hole
static void maximumArea(int l, int b,
int x, int y)
{
// Area for all the possible
// positions of the cut
int left, right, above, below;
left = x * b;
right = (l - x - 1) * b;
above = l * y;
below = (b - y - 1) * l;
// Find the maximum area
// among the above rectangles
System.out.print(Math.max(Math.max(left, right),
Math.max(above, below)));
}
// Driver Code
public static void main(String[] args)
{
int L = 8, B = 8;
int X = 0, Y = 0;
// Function call
maximumArea(L, B, X, Y);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 implementation to find area of
# largest Rectangle without hole
# within a given Rectangle
# Function to find the maximum area
# such that it does not contains any hole
def maximumArea(l, b,x, y):
# Area for all the possible
# positions of the cut
left, right, above, below = 0, 0, 0, 0
left = x * b
right = (l - x - 1) * b
above = l * y
below = (b - y - 1) * l
# Find the maximum area
# among the above rectangles
print(max(max(left, right),max(above, below)))
# Driver Code
l = 8
b = 8
x = 0
y = 0
# Function call
maximumArea(l, b, x, y)
# This code is contributed by mohit kumar 29
C#
// C# implementation to find area of
// largest Rectangle without hole
// within a given Rectangle
using System;
class GFG{
// Function to find the maximum area
// such that it does not contains any hole
static void maximumArea(int l, int b,
int x, int y)
{
// Area for all the possible
// positions of the cut
int left, right, above, below;
left = x * b;
right = (l - x - 1) * b;
above = l * y;
below = (b - y - 1) * l;
// Find the maximum area
// among the above rectangles
Console.Write(Math.Max(Math.Max(left, right),
Math.Max(above, below)));
}
// Driver Code
public static void Main(String[] args)
{
int L = 8, B = 8;
int X = 0, Y = 0;
// Function call
maximumArea(L, B, X, Y);
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// JavaScript implementation to find area of
// largest Rectangle without hole
// within a given Rectangle
// Function to find the maximum area
// such that it does not contains any hole
function maximumArea(l, b, x, y) {
// Area for all the possible
// positions of the cut
var left = x * b;
var right = (l - x - 1) * b;
var above = l * y;
var below = (b - y - 1) * l;
// Find the maximum area
// among the above rectangles
document.write(Math.max(Math.max(left, right), Math.max(above, below)));
}
// Driver Code
var L = 8,
B = 8;
var X = 0,
Y = 0;
// Function call
maximumArea(L, B, X, Y);
</script>
Performance Analysis:
- Time Complexity: There is a simple computation that does not involve any iterations or recursions. Hence, the Time Complexity will be O(1).
- Auxiliary Space Complexity: There is no extra space used. Hence, the auxiliary space complexity will be O(1).
Similar Reads
Area of the largest rectangle possible from given coordinates Given two arrays arr1[] and arr2[] of N denoting N coordinates of the form (arr1[i], 0) and M positive integers denotingM coordinates of the form (arr2[j], 1) where 1 ? i ? N and 1? j ? M. The task is to find the area of the largest rectangle that can be formed using these coordinates. Print 0 if no
8 min read
Check whether a given point lies inside a rectangle or not Given four points of a rectangle, and one more point P. Write a function to check whether P lies within the given rectangle or not.Examples:Â Â Input : R = [(10, 10), (10, -10), (-10, -10), (-10, 10)] P = (0, 0)Output : yesIllustration :Input : R = [(10, 10), (10, -10), (-10, -10), (-10, 10)], P = (20
14 min read
Ratio of area of a rectangle with the rectangle inscribed in it Given two rectangles, X with a ratio of length to width a:b and Y with a ratio of length to width c:d respectively. Both the rectangles can be resized as long as the ratio of sides remains the same. The task is to place the second rectangle inside the first rectangle such that at least 1 side is equ
7 min read
Area of largest triangle that can be inscribed within a rectangle Given a rectangle of length L     and breadth B     . The task is to find the area of the biggest triangle that can be inscribed in it.Examples:  Input: L = 5, B = 4Output: 10Input: L = 3, B = 2Output: 3  From the figure, it is clear that the largest triangle that can be inscribed in the rectangle,
4 min read
Coordinates of rectangle with given points lie inside Given two arrays X[] and Y[] with n-elements, where (Xi, Yi) represent a point on coordinate system, find the smallest rectangle such that all points from given input lie inside that rectangle and sides of rectangle must be parallel to Coordinate axis. Print all four coordinates of an obtained recta
6 min read