Javatpoint Logo
Javatpoint Logo

Find Minimum Area of Rectangle Formed from Given Shuffled Coordinates

Assuming an array arr[] of size N, the array represents N / 2 coordinates of a rectangle with its X and Y coordinates randomly shuffled. The goal of this issue is to create N / 2 pairs of (X, Y) coordinates by selecting X and Y from the array arr[] in such a way that a rectangle containing all of these points has the smallest area.

Assume we get an array of points in the XY plane. We must determine the smallest rectangle that may be created from these spots. The rectangle's sides must be parallel to the X and Y axes. If we are unable to form the rectangle, we will return 0. So, if the point array is [(1, 1), (1, 3), (3, 1), (3, 3), (2, 2)]. The result will be 4. Because the rectangle may be created by connecting the points (1, 1), (1, 3), (3, 1), and (3, 1), (3, 3).

To address the problem, follow the instructions below:

  • The array arr[] should be sorted.
  • Make a variable called ans to contain the potential answer.
  • Declare low = 1 and high = N/2 - 1 to set the window size from 0 to (N/2) - 1.
  • Begin a while loop and see if low==0. If this is the case, change the answer for the first slide.
  • However, when the slide gets the end high == N-1, break.
  • Otherwise, change the ans for the ith slide as well as the low and high pointers.

The aforesaid technique is implemented as follows:

C++ Code:

Output:

1
0

Java Program:

Output:

1
0
  • Time Complexity will be O(N).
  • Auxiliary Space will be O(1).






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA