Given an array arr[] consisting of N pair of integers representing coordinates of N points, the task is to find the area of the largest rectangle formed by straight lines drawn parallel to X and Y-axis rom given set of points.
Examples:
Input: arr[] = {{0, 0}, {1, 1}}
Output: 1
Explanation: The area of the largest rectangle is 1 formed by the coordinates (0, 0), (0, 1), (1, 0), (1, 1).Input: arr[] = {{-2, 0}, {2, 0}, {4, 0}, {4, 2}}
Output: 8
Explanation: The area of the largest rectangle possible is 8 ( length = 4 and breadth = 2 ) by the coordinates (-2, 0), (2, 0), (2, 2), (-2, 2).
Approach: The problem cn be solved using sorting technique. Follow the steps below to solve the problem:
- Store X and Y coordinates in two different arrays, say x[] and y[].
- Sort the arrays x[] and y[].
- Find the maximum adjacent difference from both arraysand store in variables X_Max and Y_Max.
- The maximum rectangle area possible is the product of X_Max and Y_Max.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to return the area of the largest // rectangle formed by lines parallel to X // and Y axis from given set of points int maxRectangle(vector<vector<int> > sequence, int size) { // Initialize two arrays long long int X_Cord[size], Y_Cord[size]; // Store x and y coordinates for (int i = 0; i < size; i++) { X_Cord[i] = sequence[i][0]; Y_Cord[i] = sequence[i][1]; } // Sort arrays sort(X_Cord, X_Cord + size); sort(Y_Cord, Y_Cord + size); // Initialize max differences long long int X_Max = 0, Y_Max = 0; // Find max adjacent differences for (int i = 0; i < size - 1; i++) { X_Max = max(X_Max, X_Cord[i + 1] - X_Cord[i]); Y_Max = max(Y_Max, Y_Cord[i + 1] - Y_Cord[i]); } // Return answer return X_Max * Y_Max; } // Driver Code int main() { // Given points vector<vector<int> > point = { { -2, 0 }, { 2, 0 }, { 4, 0 }, { 4, 2 } }; // Total points int n = point.size(); // Function call cout << maxRectangle(point, n); } |
8
Time Complexity: O(N*log(N)) where N is the total number of points.
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count rectangles generated in a given rectangle by lines drawn parallel to X and Y axis from a given set of points
- Number of lines from given N points not parallel to X or Y axis
- Program to check if the points are parallel to X axis or Y axis
- Count of Right-Angled Triangle formed from given N points whose base or perpendicular are parallel to X or Y axis
- Count of rectangles possible from N and M straight lines parallel to X and Y axis respectively
- Count squares possible from M and N straight lines parallel to X and Y axis respectively
- Count of Squares that are parallel to the coordinate axis from the given set of N points
- Find the equation of plane which passes through two points and parallel to a given axis
- Find whether only two parallel lines contain all coordinates points or not
- Number of triangles formed from a set of points on three lines
- Maximum area of a Rectangle that can be circumscribed about a given Rectangle of size LxW
- Find the point on X-axis from given N points having least Sum of Distances from all other points
- Ratio of area of a rectangle with the rectangle inscribed in it
- Minimum number of points to be removed to get remaining points on one side of axis
- Count squares of unique dimensions possible from given Straight Lines parallel to the axes
- Largest subset of rectangles such that no rectangle fit in any other rectangle
- Area of the largest Rectangle without a given point
- Area of the largest rectangle possible from given coordinates
- Number of parallelograms when n horizontal parallel lines intersect m vertical parallellines
- Distance between two parallel lines
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.

