![]() |
VOOZH | about |
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.
👁 ImageExamples:
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:
Below is the implementation of the above approach:
24
Time Complexity: O(1)
Auxiliary Space: O(1)