Area of the largest rectangle possible from given coordinates - GeeksforGeeks

07 Apr, 2021
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: 3Explanation: 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: 0Explanation: 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 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++
Java
Python3
C#
Javascript
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Function to find the maximum possible// area of a rectangleint largestArea(int arr1[], int n,                int arr2[], int m){    // Initialize variables    int end = 0, start = 0, i = 0, j = 0;     // Sort array arr1[]    sort(arr1, arr1 + n);     // Sort array arr2[]    sort(arr2, arr2 + m);     // Traverse arr1[] and arr2[]    while (i < n and j < m) {         // If arr1[i] is same as arr2[j]        if (arr1[i] == arr2[j]) {             // If no starting point            // is found yet            if (start == 0)                start = arr1[i];            else                // Update maximum end                end = arr1[i];            i++;            j++;        }         // If arr[i] > arr2[j]        else if (arr1[i] > arr2[j])            j++;        else            i++;    }     // If no rectangle is found    if (end == 0 or start == 0)        return 0;    else        // Return the area        return (end - start);} // Driver Codeint main(){    // Given point    int arr1[] = { 1, 2, 4 };     // Given length    int N = sizeof(arr1) / sizeof(arr1[0]);     // Given points    int arr2[] = { 1, 3, 4 };     // Given length    int M = sizeof(arr2) / sizeof(arr2[0]);     // Function Call    cout << largestArea(arr1, N, arr2, M);     return 0;}
// Java program for the above approachimport java.util.*; class GFG{ // Function to find the maximum possible// area of a rectanglestatic int largestArea(int arr1[], int n,                       int arr2[], int m){         // Initialize variables    int end = 0, start = 0, i = 0, j = 0;     // Sort array arr1[]    Arrays.sort(arr1);     // Sort array arr2[]    Arrays.sort(arr1);     // Traverse arr1[] and arr2[]    while (i < n && j < m)    {                 // If arr1[i] is same as arr2[j]        if (arr1[i] == arr2[j])        {                         // If no starting point            // is found yet            if (start == 0)                start = arr1[i];            else                             // Update maximum end                end = arr1[i];                             i++;            j++;        }         // If arr[i] > arr2[j]        else if (arr1[i] > arr2[j])            j++;        else            i++;    }     // If no rectangle is found    if (end == 0 || start == 0)        return 0;    else             // Return the area        return (end - start);} // Driver Codepublic static void main(String args[]){         // Given point    int arr1[] = { 1, 2, 4 };     // Given length    int N = arr1.length;     // Given points    int arr2[] = { 1, 3, 4 };     // Given length    int M = arr2.length;     // Function Call    System.out.println(largestArea(arr1, N,                                   arr2, M));}}   // This code is contributed by bolliranadheer
# Python3 program for the above approach # Function to find the maximum possible# area of a rectangledef largestArea(arr1, n, arr2, m):         # Initialize variables    end = 0    start = 0    i = 0    j = 0     # Sort array arr1[]    arr1.sort(reverse = False)     # Sort array arr2[]    arr2.sort(reverse = False)     # Traverse arr1[] and arr2[]    while (i < n and j < m):                 # If arr1[i] is same as arr2[j]        if (arr1[i] == arr2[j]):                         # If no starting point            # is found yet            if (start == 0):                start = arr1[i]            else:                                 # Update maximum end                end = arr1[i]                             i += 1            j += 1         # If arr[i] > arr2[j]        elif (arr1[i] > arr2[j]):            j += 1        else:            i += 1     # If no rectangle is found    if (end == 0 or start == 0):        return 0    else:                 # Return the area        return (end - start) # Driver Codeif __name__ == '__main__':         # Given point    arr1 = [ 1, 2, 4 ]     # Given length    N = len(arr1)     # Given points    arr2 = [ 1, 3, 4 ]     # Given length    M =  len(arr2)     # Function Call    print(largestArea(arr1, N, arr2, M)) # This code is contributed by ipg2016107
// C# program for the above approachusing System; class GFG{     // Function to find the maximum possible// area of a rectanglestatic int largestArea(int[] arr1, int n,                       int[] arr2, int m){         // Initialize variables    int end = 0, start = 0, i = 0, j = 0;      // Sort array arr1[]    Array.Sort(arr1);         // Sort array arr2[]    Array.Sort(arr2);      // Traverse arr1[] and arr2[]    while (i < n && j < m)    {                 // If arr1[i] is same as arr2[j]        if (arr1[i] == arr2[j])        {                         // If no starting point            // is found yet            if (start == 0)                start = arr1[i];            else                             // Update maximum end                end = arr1[i];                              i++;            j++;        }          // If arr[i] > arr2[j]        else if (arr1[i] > arr2[j])            j++;        else            i++;    }      // If no rectangle is found    if (end == 0 || start == 0)        return 0;    else              // Return the area        return (end - start);} // Driver codestatic void Main(){         // Given point    int[] arr1 = { 1, 2, 4 };      // Given length    int N = arr1.Length;      // Given points    int[] arr2 = { 1, 3, 4 };      // Given length    int M = arr2.Length;      // Function Call    Console.WriteLine(largestArea(arr1, N,                                  arr2, M));}} // This code is contributed by divyeshrabadiya07
<script> // Javascript program for the above approach // Function to find the maximum possible// area of a rectanglefunction largestArea(arr1, n,                    arr2, m){     // Initialize variables    var end = 0, start = 0, i = 0, j = 0;     // Sort array arr1[]    arr1.sort();     // Sort array arr2[]    arr2.sort();     // Traverse arr1[] and arr2[]    while (i < n && j < m) {         // If arr1[i] is same as arr2[j]        if (arr1[i] == arr2[j]) {             // If no starting point            // is found yet            if (start == 0)                start = arr1[i];            else                // Update maximum end                end = arr1[i];            i++;            j++;        }         // If arr[i] > arr2[j]        else if (arr1[i] > arr2[j])            j++;        else            i++;    }     // If no rectangle is found    if (end == 0 || start == 0)        return 0;    else        // Return the area        return (end - start);} // Driver Code // Given pointvar arr1 = [ 1, 2, 4 ]; // Given lengthvar N = arr1.length; // Given pointsvar arr2 = [ 1, 3, 4 ]; // Given lengthvar M = arr2.length; // Function Calldocument.write(largestArea(arr1, N, arr2, M)); // This code is contributed by noob2000.</script>
3
 
Time Complexity: O(N*log N + M*log M)Auxiliary Space: O(M+N)
bolliranadheer
ipg2016107
divyeshrabadiya07
noob2000
Arrays
Geometric
Sorting
Arrays
Sorting
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Introduction to Arrays
Multidimensional Arrays in Java
Linear Search
Closest Pair of Points using Divide and Conquer algorithm
How to check if a given point lies inside or outside a polygon?
Program for distance between two points on earth
How to check if two given line segments intersect?
Find if two rectangles overlap