![]() |
VOOZH | about |
Given a number N, and an array A[] of size 2N, the task is to make N pairs of these array elements and place them on an X-Y coordinate plane, such that they are enclosed inside a minimum area rectangle( with sides parallel to the X-axis and Y-axis) and print the area of the rectangle.
Examples:
Input: N = 4, A = {1, 4, 2, 5, 3, 6, 7, 8}
👁 Image
Output: 9
Explanation: One possible way of making N pairs to get minimum rectangle area is {(1, 5), (2, 7), (3, 6), (4, 8)}
The minimum area rectangle has been shown in the following diagram:
Note: There maybe other ways to make N pairs such that the area remains minimum, but the minimum area remains 9.
Input: N = 3, A = {1, 3, 1, 1, 2, 1}
Output: 0
Approach: The area of the rectangle with the bottom-left corner in (X1, Y1) and top-right corner in (X2, Y2) would be (X2 - X1)*(Y2 - Y1). Thus, the task can be presented as partitioning the array A into two N-sized partitions say X and Y, such that (Max(X) - Min(X)) * (Max(Y) - Min(Y)) is minimized. Here, X represents the X-coordinates of the pairs and Y represents the Y-coordinates.
After sorting A, the minimum would be A1, and the maximum would be A2N. Now, there can be the following two cases:
Follow the steps below to solve the problem:
Below is an implementation of the above code:
9
Time Complexity: O(NLogN)
Auxiliary Space: O(1)