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 from a 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 can be solved using the 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 arrays and 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++
#include <bits/stdc++.h>
using namespace std;
int maxRectangle(vector<vector< int > > sequence,
int size)
{
long long int X_Cord[size], Y_Cord[size];
for ( int i = 0; i < size; i++) {
X_Cord[i] = sequence[i][0];
Y_Cord[i] = sequence[i][1];
}
sort(X_Cord, X_Cord + size);
sort(Y_Cord, Y_Cord + size);
long long int X_Max = 0, Y_Max = 0;
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 X_Max * Y_Max;
}
int main()
{
vector<vector< int > > point
= { { -2, 0 }, { 2, 0 }, { 4, 0 }, { 4, 2 } };
int n = point.size();
cout << maxRectangle(point, n);
}
|
Java
import java.util.*;
class GFG{
static int maxRectangle( int [][] sequence,
int size)
{
int [] X_Cord = new int [size];
int [] Y_Cord = new int [size];
for ( int i = 0 ; i < size; i++)
{
X_Cord[i] = sequence[i][ 0 ];
Y_Cord[i] = sequence[i][ 1 ];
}
Arrays.sort(X_Cord);
Arrays.sort(Y_Cord);
int X_Max = 0 , Y_Max = 0 ;
for ( int i = 0 ; i < size - 1 ; i++)
{
X_Max = Math.max(X_Max, X_Cord[i + 1 ] -
X_Cord[i]);
Y_Max = Math.max(Y_Max, Y_Cord[i + 1 ] -
Y_Cord[i]);
}
return X_Max * Y_Max;
}
public static void main(String[] args)
{
int [][] point = { { - 2 , 0 }, { 2 , 0 },
{ 4 , 0 }, { 4 , 2 } };
int n = point.length;
System.out.print(maxRectangle(point, n));
}
}
|
Python3
def maxRectangle(sequence, size):
X_Cord = [ 0 ] * size
Y_Cord = [ 0 ] * size
for i in range (size):
X_Cord[i] = sequence[i][ 0 ]
Y_Cord[i] = sequence[i][ 1 ]
X_Cord.sort()
Y_Cord.sort()
X_Max = 0
Y_Max = 0
for i in range (size - 1 ):
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 X_Max * Y_Max
if __name__ = = "__main__" :
point = [[ - 2 , 0 ], [ 2 , 0 ],
[ 4 , 0 ], [ 4 , 2 ]]
n = len (point)
print (maxRectangle(point, n))
|
C#
using System;
class GFG{
static int maxRectangle( int [,] sequence,
int size)
{
int [] X_Cord = new int [size];
int [] Y_Cord = new int [size];
for ( int i = 0; i < size; i++)
{
X_Cord[i] = sequence[i, 0];
Y_Cord[i] = sequence[i, 1];
}
Array.Sort(X_Cord);
Array.Sort(Y_Cord);
int X_Max = 0, Y_Max = 0;
for ( int i = 0; i < size - 1; i++)
{
X_Max = Math.Max(X_Max, X_Cord[i + 1] -
X_Cord[i]);
Y_Max = Math.Max(Y_Max, Y_Cord[i + 1] -
Y_Cord[i]);
}
return X_Max * Y_Max;
}
public static void Main(String[] args)
{
int [,] point = { { -2, 0 }, { 2, 0 },
{ 4, 0 }, { 4, 2 } };
int n = point.GetLength(0);
Console.Write(maxRectangle(point, n));
}
}
|
Javascript
<script>
function maxRectangle(sequence, size)
{
let X_Cord = [];
let Y_Cord = [];
for (let i = 0; i < size; i++)
{
X_Cord[i] = sequence[i][0];
Y_Cord[i] = sequence[i][1];
}
X_Cord.sort();
Y_Cord.sort();
let X_Max = 0, Y_Max = 0;
for (let i = 0; i < size - 1; i++)
{
X_Max = Math.max(X_Max, X_Cord[i + 1] -
X_Cord[i]);
Y_Max = Math.max(Y_Max, Y_Cord[i + 1] -
Y_Cord[i]);
}
return X_Max * Y_Max;
}
let point = [[ -2, 0 ], [ 2, 0 ],
[ 4, 0 ], [ 4, 2 ]];
let n = point.length;
document.write(maxRectangle(point, n));
</script>
|
Time Complexity: O(N*log(N)) where N is the total number of points.
Auxiliary Space: O(N)
Last Updated :
11 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...