Given two arrays X[] and Y[], each of length 4, where (X[0], Y[0]) and (X[1], Y[1]) represents the bottom left and top right corners of one rectangle and (X[2], Y[2]) and (X[3], Y[3]) represents the bottom left and top right corners of the other rectangle, the task is to find the perimeter of the outer boundaries of the union of the two rectangles as shown below.
Examples:
Input: X[] = {-1, 2, 0, 4}, Y[] = {2, 5, -3, 3}
Output: 26
Explanation: Required Perimeter = 2 * ( (4 – (-1)) + (5 – (-3)) ) = 2*(8 + 5) = 26.Input: X[] = {-3, 1, 1, 4}, Y[] = {-2, 3, 1, 5}
Output: 26
Explanation: Required Perimeter = 2 * ( (4 – (-3)) + (5 – (-2)) ) = 2*(7 + 7) = 28.
Approach: Follow the steps below to solve the problem:
- Check if the rectangles formed by the given points intersect or not.
- If found to be intersecting, then the perimeter can be calculated by the formula 2*((X[1] – X[0]) + (X[3] – X[2]) + (Y[1] – Y[0]) + (Y[3] – Y[2])).
- Otherwise, print twice the sum of maximum differences between X and Y coordinates respectively, i.e. 2 * (max(X[]) – min(X[]) + max(Y[]) – min(Y[])).
Below is the implementation of the above approach:
C++
// C++ program for the above approach Â
#include <bits/stdc++.h> using namespace std; Â
// Function to check if two // rectangles are intersecting or not bool doIntersect(vector< int > X,                  vector< int > Y) {     // If one rectangle is to the     // right of other's right edge     if (X[0] > X[3] || X[2] > X[1])         return false ; Â
    // If one rectangle is on the     // top of other's top edge     if (Y[0] > Y[3] || Y[2] > Y[1])         return false ; Â
    return true ; } Â
// Function to return the perimeter of // the Union of Two Rectangles int getUnionPerimeter(vector< int > X,                       vector< int > Y) {     // Stores the resultant perimeter     int perimeter = 0; Â
    // If rectangles do not interesect     if (!doIntersect(X, Y)) { Â
        // Perimeter of Rectangle 1         perimeter             += 2 * ( abs (X[1] - X[0])                     + abs (Y[1] - Y[0])); Â
        // Perimeter of Rectangle 2         perimeter             += 2 * ( abs (X[3] - X[2])                     + abs (Y[3] - Y[2]));     } Â
    // If the rectangles intersect     else { Â
        // Get width of combined figure         int w = *max_element(X.begin(),                              X.end())                 - *min_element(X.begin(),                                X.end()); Â
        // Get length of combined figure         int l = *max_element(Y.begin(),                              Y.end())                 - *min_element(Y.begin(),                                Y.end()); Â
        perimeter = 2 * (l + w);     } Â
    // Return the perimeter     return perimeter; } Â
// Driver Code int main() { Â Â Â Â vector< int > X{ -1, 2, 4, 6 }; Â Â Â Â vector< int > Y{ 2, 5, 3, 7 }; Â
    cout << getUnionPerimeter(X, Y); } |
Java
// Java program for the above approach import java.util.*; class GFG { Â
// Function to check if two // rectangles are intersecting or not static boolean doIntersect( int []X,                  int []Y) {     // If one rectangle is to the     // right of other's right edge     if (X[ 0 ] > X[ 3 ] || X[ 2 ] > X[ 1 ])         return false ; Â
    // If one rectangle is on the     // top of other's top edge     if (Y[ 0 ] > Y[ 3 ] || Y[ 2 ] > Y[ 1 ])         return false ; Â
    return true ; } Â
// Function to return the perimeter of // the Union of Two Rectangles static int getUnionPerimeter( int []X,                       int []Y) {     // Stores the resultant perimeter     int perimeter = 0 ; Â
    // If rectangles do not interesect     if (!doIntersect(X, Y)) { Â
        // Perimeter of Rectangle 1         perimeter             += 2 * (Math.abs(X[ 1 ] - X[ 0 ])                     + Math.abs(Y[ 1 ] - Y[ 0 ])); Â
        // Perimeter of Rectangle 2         perimeter             += 2 * (Math.abs(X[ 3 ] - X[ 2 ])                     + Math.abs(Y[ 3 ] - Y[ 2 ]));     } Â
    // If the rectangles intersect     else { Â
        // Get width of combined figure         int w = Arrays.stream(X).max().getAsInt()                 - Arrays.stream(X).min().getAsInt(); Â
        // Get length of combined figure         int l = Arrays.stream(Y).max().getAsInt()                 - Arrays.stream(Y).min().getAsInt(); Â
        perimeter = 2 * (l + w);     } Â
    // Return the perimeter     return perimeter; } Â
// Driver Code public static void main(String[] args) { Â Â Â Â int []X = { - 1 , 2 , 4 , 6 }; Â Â Â Â int []Y = { 2 , 5 , 3 , 7 }; Â
    System.out.print(getUnionPerimeter(X, Y)); } } Â
// This code is contributed by 29AjayKumar |
Python3
# Python3 program for the above approach Â
# Function to check if two # rectangles are intersecting or not def doIntersect(X, Y):          # If one rectangle is to the     # right of other's right edge     if (X[ 0 ] > X[ 3 ] or X[ 2 ] > X[ 1 ]):         return False Â
    # If one rectangle is on the     # top of other's top edge     if (Y[ 0 ] > Y[ 3 ] or Y[ 2 ] > Y[ 1 ]):         return False     return True Â
# Function to return the perimeter of # the Union of Two Rectangles def getUnionPerimeter(X, Y):        # Stores the resultant perimeter     perimeter = 0 Â
    # If rectangles do not interesect     if ( not doIntersect(X, Y)): Â
        # Perimeter of Rectangle 1         perimeter + = 2 * ( abs (X[ 1 ] - X[ 0 ]) + abs (Y[ 1 ] - Y[ 0 ])) Â
        # Perimeter of Rectangle 2         perimeter + = 2 * ( abs (X[ 3 ] - X[ 2 ]) + abs (Y[ 3 ] - Y[ 2 ]))          # If the rectangles intersect     else : Â
        # Get width of combined figure         w = max (X) -  min (X)                  # Get length of combined figure         l = max (Y) - min (Y)         perimeter = 2 * (l + w) Â
    # Return the perimeter     return perimeter Â
# Driver Code if __name__ = = '__main__' : Â Â Â Â X = [ - 1 , 2 , 4 , 6 ] Â Â Â Â Y = [ 2 , 5 , 3 , 7 ] Â
    print (getUnionPerimeter(X, Y)) Â
# This code is contributed by mohit kumar 29. |
C#
// C# program for the above approach using System; using System.Linq; public class GFG { Â
// Function to check if two // rectangles are intersecting or not static bool doIntersect( int []X,                  int []Y) {        // If one rectangle is to the     // right of other's right edge     if (X[0] > X[3] || X[2] > X[1])         return false ; Â
    // If one rectangle is on the     // top of other's top edge     if (Y[0] > Y[3] || Y[2] > Y[1])         return false ; Â
    return true ; } Â
// Function to return the perimeter of // the Union of Two Rectangles static int getUnionPerimeter( int []X,                       int []Y) {        // Stores the resultant perimeter     int perimeter = 0; Â
    // If rectangles do not interesect     if (!doIntersect(X, Y))     { Â
        // Perimeter of Rectangle 1         perimeter             += 2 * (Math.Abs(X[1] - X[0])                     + Math.Abs(Y[1] - Y[0])); Â
        // Perimeter of Rectangle 2         perimeter             += 2 * (Math.Abs(X[3] - X[2])                     + Math.Abs(Y[3] - Y[2]));     } Â
    // If the rectangles intersect     else     { Â
        // Get width of combined figure         int w = X.Max()                 - X.Min(); Â
        // Get length of combined figure         int l = X.Max()                 - Y.Min(); Â
        perimeter = 2 * (l + w);     } Â
    // Return the perimeter     return perimeter; } Â
// Driver Code public static void Main(String[] args) { Â Â Â Â int []X = { -1, 2, 4, 6 }; Â Â Â Â int []Y = { 2, 5, 3, 7 }; Â
    Console.Write(getUnionPerimeter(X, Y)); } } Â
// This code contributed by shikhasingrajput |
Javascript
<script> // Javascript program for the above approach Â
// Function to check if two // rectangles are intersecting or not function doIntersect(X,Y) {     // If one rectangle is to the     // right of other's right edge     if (X[0] > X[3] || X[2] > X[1])         return false ;       // If one rectangle is on the     // top of other's top edge     if (Y[0] > Y[3] || Y[2] > Y[1])         return false ;       return true ; } Â
// Function to return the perimeter of // the Union of Two Rectangles function getUnionPerimeter(X,Y) {     // Stores the resultant perimeter     let perimeter = 0;       // If rectangles do not interesect     if (!doIntersect(X, Y)) {           // Perimeter of Rectangle 1         perimeter             += 2 * (Math.abs(X[1] - X[0])                     + Math.abs(Y[1] - Y[0]));           // Perimeter of Rectangle 2         perimeter             += 2 * (Math.abs(X[3] - X[2])                     + Math.abs(Y[3] - Y[2]));     }       // If the rectangles intersect     else {           // Get width of combined figure         let w = Math.max(...X)                 - Math.min(...X);           // Get length of combined figure         let l = Math.max(...Y)                 - Math.min(...Y);           perimeter = 2 * (l + w);     }       // Return the perimeter     return perimeter; } Â
// Driver Code let X = [-1, 2, 4, 6 ]; let Y = [ 2, 5, 3, 7 ]; document.write(getUnionPerimeter(X, Y)); Â
// This code is contributed by patel2127 </script> |
24
Â
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!