Area of the largest rectangle formed by lines parallel to X and Y axis from given set of points

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:



  1. Store X and Y coordinates in two different arrays, say x[] and y[].
  2. Sort the arrays x[] and y[].
  3. Find the maximum adjacent difference from both arraysand store in variables X_Max and Y_Max.
  4. The maximum rectangle area possible is the product of X_Max and Y_Max.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
}

chevron_right


Output:

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.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

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.