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 rectangle can be formed.
Examples:
Input: arr1[] = {1, 2, 4}, arr2[] = {1, 3, 4}
Output: 3
Explanation: The largest rectangle possible is {(1, 0), (1, 1), (4, 0), (4, 1)}.
Therefore, area of rectangle = length * breadth = (4 – 1)*(1 – 0) = 3
Input: arr1[] = {1, 3, 5}, arr2[] = {4, 2, 10}
Output: 0
Explanation: No rectangle can be formed. Therefore, the answer is zero.
Naive Approach: The simplest approach is to traverse the given array arr1[] and for each i, traverse the points in arr2[] using a variable j. If arr1[i] is equal to arr2[j], then store the value arr1[i] in the separate array, say ans[]. After finding all such values, sort the ans[] array and print the maximum area as ans[L] – ans[0] where L is the index of the last element of ans[] as the breadth of the rectangle will always be 1.
Time Complexity: O(N*M)
Auxiliary Space: O(M + N)
Efficient Approach: The idea is to use the Sorting Algorithm and Two-Pointer Technique. Observe that the breadth of the rectangle will always be 1. Therefore, the maximum area can be found by maximizing its length. Follow the below steps to solve the problem:
- Sort the given arrays arr1[] and arr2[].
- Initialize the variables, start and end with 0 to store the starting and ending point of the length. Also, initialize the variables i and j to traverse the array arr1[] and arr2[] respectively.
- While i is smaller than N and j smaller than M, check the following conditions:
- If arr1[i] is the same as arr2[j] and start is 0, update start as start = arr1[i]. Otherwise, update end as end = arr1[i] then increment i and j by 1.
- If arr1[i] is greater than arr2[j], increment j by 1. Otherwise, increment i by 1.
- If start or end is 0, print 0. Otherwise, print (end – start) as the maximum possible area.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int largestArea( int arr1[], int n,
int arr2[], int m)
{
int end = 0, start = 0, i = 0, j = 0;
sort(arr1, arr1 + n);
sort(arr2, arr2 + m);
while (i < n and j < m) {
if (arr1[i] == arr2[j]) {
if (start == 0)
start = arr1[i];
else
end = arr1[i];
i++;
j++;
}
else if (arr1[i] > arr2[j])
j++;
else
i++;
}
if (end == 0 or start == 0)
return 0;
else
return (end - start);
}
int main()
{
int arr1[] = { 1, 2, 4 };
int N = sizeof (arr1) / sizeof (arr1[0]);
int arr2[] = { 1, 3, 4 };
int M = sizeof (arr2) / sizeof (arr2[0]);
cout << largestArea(arr1, N, arr2, M);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int largestArea( int arr1[], int n,
int arr2[], int m)
{
int end = 0 , start = 0 , i = 0 , j = 0 ;
Arrays.sort(arr1);
Arrays.sort(arr1);
while (i < n && j < m)
{
if (arr1[i] == arr2[j])
{
if (start == 0 )
start = arr1[i];
else
end = arr1[i];
i++;
j++;
}
else if (arr1[i] > arr2[j])
j++;
else
i++;
}
if (end == 0 || start == 0 )
return 0 ;
else
return (end - start);
}
public static void main(String args[])
{
int arr1[] = { 1 , 2 , 4 };
int N = arr1.length;
int arr2[] = { 1 , 3 , 4 };
int M = arr2.length;
System.out.println(largestArea(arr1, N,
arr2, M));
}
}
|
Python3
def largestArea(arr1, n, arr2, m):
end = 0
start = 0
i = 0
j = 0
arr1.sort(reverse = False )
arr2.sort(reverse = False )
while (i < n and j < m):
if (arr1[i] = = arr2[j]):
if (start = = 0 ):
start = arr1[i]
else :
end = arr1[i]
i + = 1
j + = 1
elif (arr1[i] > arr2[j]):
j + = 1
else :
i + = 1
if (end = = 0 or start = = 0 ):
return 0
else :
return (end - start)
if __name__ = = '__main__' :
arr1 = [ 1 , 2 , 4 ]
N = len (arr1)
arr2 = [ 1 , 3 , 4 ]
M = len (arr2)
print (largestArea(arr1, N, arr2, M))
|
C#
using System;
class GFG{
static int largestArea( int [] arr1, int n,
int [] arr2, int m)
{
int end = 0, start = 0, i = 0, j = 0;
Array.Sort(arr1);
Array.Sort(arr2);
while (i < n && j < m)
{
if (arr1[i] == arr2[j])
{
if (start == 0)
start = arr1[i];
else
end = arr1[i];
i++;
j++;
}
else if (arr1[i] > arr2[j])
j++;
else
i++;
}
if (end == 0 || start == 0)
return 0;
else
return (end - start);
}
static void Main()
{
int [] arr1 = { 1, 2, 4 };
int N = arr1.Length;
int [] arr2 = { 1, 3, 4 };
int M = arr2.Length;
Console.WriteLine(largestArea(arr1, N,
arr2, M));
}
}
|
Javascript
<script>
function largestArea(arr1, n,
arr2, m)
{
var end = 0, start = 0, i = 0, j = 0;
arr1.sort();
arr2.sort();
while (i < n && j < m) {
if (arr1[i] == arr2[j]) {
if (start == 0)
start = arr1[i];
else
end = arr1[i];
i++;
j++;
}
else if (arr1[i] > arr2[j])
j++;
else
i++;
}
if (end == 0 || start == 0)
return 0;
else
return (end - start);
}
var arr1 = [ 1, 2, 4 ];
var N = arr1.length;
var arr2 = [ 1, 3, 4 ];
var M = arr2.length;
document.write(largestArea(arr1, N, arr2, M));
</script>
|
Time Complexity: O(N*log N + M*log M)
Auxiliary Space: O(M+N)
Last Updated :
07 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...