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.
Input: L = 8, B = 8, X = 0, Y = 0
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
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:
- Time Complexity: There is a simple computation which does not involves 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).
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Area of Largest rectangle that can be inscribed in an Ellipse
- Area of largest triangle that can be inscribed within a rectangle
- Ratio of area of a rectangle with the rectangle inscribed in it
- Largest subset of rectangles such that no rectangle fit in any other rectangle
- Finding the best fit rectangle that covers a given point
- Area and Perimeter of Rectangle in PL/SQL
- Check if any point overlaps the given Circle and Rectangle
- Check if a point lies on or inside a rectangle | Set-2
- Check whether a given point lies on or inside the rectangle | Set 3
- Check whether a given point lies inside a rectangle or not
- Maximum area of rectangle possible with given perimeter
- Program for Area And Perimeter Of Rectangle
- Sum of Area of all possible square inside a rectangle
- Area of the biggest ellipse inscribed within a rectangle
- Number of squares of maximum area in a rectangle
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.