# Make N pairs from Array as (X, Y) coordinate point that are enclosed inside a minimum area rectangle

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}Output:9Explanation: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 **(X _{1}, Y_{1})** and top-right corner in (

**X**would be

_{2}, Y_{2})**(X**Thus, the task can be presented as partitioning the array

_{2 }– X_{1})*(Y_{2 }– Y_{1}).**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 **A _{1}**, and the maximum would be

**A**

_{2N}_{.}Now, there can be the following two cases:

- Both
**A**and_{1 }**A**are present in the same partition, say_{2N}**X**.The area of the rectangle would be**(A**Then the task would be to minimize_{2N}– A_{1}) * (Max(Y) – Min(Y)).**Max(Y) – Min(Y).**Also, if**i**is the index of**Min(Y)**and**j**is the index of**Max(Y),**then**j – i >= N – 1**, as there has to be at least**N**elements in**Y**. Thus, it is optimal to use a segment of size**N**for**Y**(barring**A**and_{1}**A**, as they already have been taken),_{2N} **A**and_{1}**A**are present in different partitions. For this case, it would be optimal to use a prefix of size_{2N}**N**, and a suffix of size**N**as the partitions, i.e placing the first**N**elements in one partition and the last**N**elements in the other._{ }

Follow the steps below to solve the problem:

- Initialize a variable say,
**ans**to store the minimum area of the rectangle. - Sort the array
**A[]**in ascending order. **For the first case**:- Update
**ans**as**(A[N – 1] – A[0]) * (A[2 * N – 1] – A[N]).**

- Update
**For the second case**:- Iterate in the range
**[1, N-1]**using the variable**i**:- Update
**ans**as the minimum of**ans**and**(A[2 * N – 1] – A[0]) * (A[i + N – 1] – A[i]).**

- Update

- Iterate in the range
- Finally, return
**ans.**

Below is an implementation of the above code:

## C++

`// C++ program for tha above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to make N pairs of coordinates such that they` `// are enclosed in a minimum area rectangle with sides` `// parallel to the X and Y axes` `int` `minimumRectangleArea(` `int` `A[], ` `int` `N)` `{` ` ` `// A variable to store the answer` ` ` `int` `ans;` ` ` `sort(A, A + 2 * N);` ` ` `// For the case where the maximum` ` ` `// and minimum are in different partitions` ` ` `ans = (A[N - 1] - A[0]) * (A[2 * N - 1] - A[N]);` ` ` `// For the case where the maximum and` ` ` `// minimum are in the same partition` ` ` `for` `(` `int` `i = 1; i < N; i++)` ` ` `ans = min(ans, (A[2 * N - 1] - A[0])` ` ` `* (A[i + N - 1] - A[i]));` ` ` `// Return the answer` ` ` `return` `ans;` `}` `// Driver code` `int` `main()` `{` ` ` `// Given Input` ` ` `int` `A[] = { 2, 4, 1, 5, 3, 6, 7, 8 };` ` ` `int` `N = ` `sizeof` `(A) / ` `sizeof` `(A[0]);` ` ` `N /= 2;` ` ` `// Function call` ` ` `cout << minimumRectangleArea(A, N) << endl;` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `import` `java.io.*;` `import` `java.util.Arrays;` `class` `GFG{` ` ` `// Function to make N pairs of coordinates` `// such that they are enclosed in a minimum` `// area rectangle with sides parallel to` `// the X and Y axes` `public` `static` `int` `minimumRectangleArea(` `int` `A[], ` `int` `N)` `{` ` ` ` ` `// A variable to store the answer` ` ` `int` `ans;` ` ` `Arrays.sort(A);` ` ` `// For the case where the maximum` ` ` `// and minimum are in different partitions` ` ` `ans = (A[N - ` `1` `] - A[` `0` `]) * (A[` `2` `* N - ` `1` `] - A[N]);` ` ` `// For the case where the maximum and` ` ` `// minimum are in the same partition` ` ` `for` `(` `int` `i = ` `1` `; i < N; i++)` ` ` `ans = Math.min(ans, (A[` `2` `* N - ` `1` `] - A[` `0` `]) *` ` ` `(A[i + N - ` `1` `] - A[i]));` ` ` `// Return the answer` ` ` `return` `ans;` `}` `// Driver code` `public` `static` `void` `main(String[] args)` `{` ` ` ` ` `// Given Input` ` ` `int` `A[] = { ` `2` `, ` `4` `, ` `1` `, ` `5` `, ` `3` `, ` `6` `, ` `7` `, ` `8` `};` ` ` `int` `N = A.length;` ` ` `N = (` `int` `)N / ` `2` `;` ` ` `// Function call` ` ` `System.out.println(minimumRectangleArea(A, N));` `}` `}` `// This code is contributed by lokeshpotta20` |

## Python3

`# Python3 program for tha above approach` `# Function to make N pairs of coordinates` `# such that they are enclosed in a minimum` `# area rectangle with sides parallel to the` `# X and Y axes` `def` `minimumRectangleArea(A, N):` ` ` ` ` `# A variable to store the answer` ` ` `ans ` `=` `0` ` ` `A.sort()` ` ` `# For the case where the maximum` ` ` `# and minimum are in different partitions` ` ` `ans ` `=` `(A[N ` `-` `1` `] ` `-` `A[` `0` `]) ` `*` `(A[` `2` `*` `N ` `-` `1` `] ` `-` `A[N])` ` ` `# For the case where the maximum and` ` ` `# minimum are in the same partition` ` ` `for` `i ` `in` `range` `(` `1` `, N, ` `1` `):` ` ` `ans ` `=` `min` `(ans, (A[` `2` `*` `N ` `-` `1` `] ` `-` `A[` `0` `]) ` `*` ` ` `(A[i ` `+` `N ` `-` `1` `] ` `-` `A[i]))` ` ` `# Return the answer` ` ` `return` `ans` `# Driver code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `# Given Input` ` ` `A ` `=` `[ ` `2` `, ` `4` `, ` `1` `, ` `5` `, ` `3` `, ` `6` `, ` `7` `, ` `8` `]` ` ` `N ` `=` `len` `(A)` ` ` `N ` `/` `/` `=` `2` ` ` `# Function call` ` ` `print` `(minimumRectangleArea(A, N))` ` ` `# This code is contributed by ipg2016107` |

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG{` ` ` `// Function to make N pairs of coordinates` `// such that they are enclosed in a minimum` `// area rectangle with sides parallel to` `// the X and Y axes` `public` `static` `int` `minimumRectangleArea(` `int` `[]A, ` `int` `N)` `{` ` ` ` ` `// A variable to store the answer` ` ` `int` `ans;` ` ` `Array.Sort(A);` ` ` `// For the case where the maximum` ` ` `// and minimum are in different partitions` ` ` `ans = (A[N - 1] - A[0]) * (A[2 * N - 1] - A[N]);` ` ` `// For the case where the maximum and` ` ` `// minimum are in the same partition` ` ` `for` `(` `int` `i = 1; i < N; i++)` ` ` `ans = Math.Min(ans, (A[2 * N - 1] - A[0]) *` ` ` `(A[i + N - 1] - A[i]));` ` ` `// Return the answer` ` ` `return` `ans;` `}` `// Driver code` `public` `static` `void` `Main(String[] args)` `{` ` ` ` ` `// Given Input` ` ` `int` `[]A = { 2, 4, 1, 5, 3, 6, 7, 8 };` ` ` `int` `N = A.Length;` ` ` `N = (` `int` `)N / 2;` ` ` `// Function call` ` ` `Console.Write(minimumRectangleArea(A, N));` `}` `}` `// This code is contributed by shivanisinghss2110` |

## Javascript

`<script>` `// JavaScript program for tha above approach` `// Function to make N pairs of coordinates such that they` `// are enclosed in a minimum area rectangle with sides` `// parallel to the X and Y axes` `function` `minimumRectangleArea(A, N) {` `// A variable to store the answer` ` ` `let ans;` ` ` `A.sort();` ` ` `// For the case where the maximum` ` ` `// and minimum are in different partitions` ` ` `ans = (A[N - 1] - A[0]) * (A[2 * N - 1] - A[N]);` ` ` `// For the case where the maximum and` ` ` `// minimum are in the same partition` ` ` `for` `(let i = 1; i < N; i++)` ` ` `ans = Math.min(ans, (A[2 * N - 1] - A[0])` ` ` `* (A[i + N - 1] - A[i]));` ` ` `// Return the answer` ` ` `return` `ans;` `}` `// Driver code` `// Given Input` `let A = [2, 4, 1, 5, 3, 6, 7, 8];` `let N = A.length` `N = Math.floor(N / 2);` `// Function call` `document.write(minimumRectangleArea(A, N) + ` `"<br>"` `);` `</script>` |

**Output**

9

**Time Complexity: **O(NLogN)**Auxiliary Space: **O(1)