Given an array **arr[]** of positive integers of even length, the task is to partition these elements of **arr[]** into two groups each of odd length such that the absolute difference between the median of the two groups is minimized.

**Examples:**

Input:arr[] = { 1, 2, 3, 4, 5, 6 }

Output:1

Explanation:

Group 1 can be [2, 4, 6] with median 4

Group 2 can be [1, 3, 5] with median 3.

The absolute difference between the two medians is 4 – 3 = 1, which can’t be minimized further with any kind of groupings formed.

Input:arr[] = { 15, 25, 35, 50 }

Output:10

Explanation:

Group 1 can be [15, 25, 50] with median 25

Group 2 can be [35] with median 35.

The absolute difference between the two medians is 35 – 25 = 10, which can’t be minimized further with any kind of groupings formed.

**Approach:**

- If the given array
**arr[]**is sorted, the middle elements of**arr[]**will give the minimum difference. - So divide the
**arr[]**in such a way that these two elements will be the median of two new arrays of odd length. - Therefore, put the
**n/2**element of the arr[] in the first group and the^{th}**(n/2 – 1)**element of the^{th}**arr[]**in the second group as a median respectively. - Then
**abs(arr[n/2] – arr[(n/2)-1])**is the minimum difference between the two new arrays.

Below is the implementation of the above approach:

## C++

`// C++ program to minimise the ` `// median between partition array ` ` ` `#include "bits/stdc++.h" ` `using` `namespace` `std; ` ` ` `// Function to find minimise the ` `// median between partition array ` `int` `minimiseMedian(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// Sort the given array arr[] ` ` ` `sort(arr, arr + n); ` ` ` ` ` `// Return the difference of two ` ` ` `// middle element of the arr[] ` ` ` `return` `abs` `(arr[n / 2] - arr[(n / 2) - 1]); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 15, 25, 35, 50 }; ` ` ` ` ` `// Size of arr[] ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `// Function that returns the minimum ` ` ` `// the absolute difference between ` ` ` `// median of partition array ` ` ` `cout << minimiseMedian(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to minimise the ` `// median between partition array ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find minimise the ` ` ` `// median between partition array ` ` ` `static` `int` `minimiseMedian(` `int` `arr[], ` `int` `n) ` ` ` `{ ` ` ` `// Sort the given array arr[] ` ` ` `Arrays.sort(arr); ` ` ` ` ` `// Return the difference of two ` ` ` `// middle element of the arr[] ` ` ` `return` `Math.abs(arr[n / ` `2` `] - arr[(n / ` `2` `) - ` `1` `]); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `arr[] = { ` `15` `, ` `25` `, ` `35` `, ` `50` `}; ` ` ` ` ` `// Size of arr[] ` ` ` `int` `n = arr.length; ` ` ` ` ` `// Function that returns the minimum ` ` ` `// the absolute difference between ` ` ` `// median of partition array ` ` ` `System.out.println(minimiseMedian(arr, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to minimise the ` `# median between partition array ` ` ` `# Function to find minimise the ` `# median between partition array ` `def` `minimiseMedian(arr, n) : ` ` ` ` ` `# Sort the given array arr[] ` ` ` `arr.sort(); ` ` ` ` ` `# Return the difference of two ` ` ` `# middle element of the arr[] ` ` ` `ans ` `=` `abs` `(arr[n ` `/` `/` `2` `] ` `-` `arr[(n ` `/` `/` `2` `) ` `-` `1` `]); ` ` ` ` ` `return` `ans; ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `arr ` `=` `[ ` `15` `, ` `25` `, ` `35` `, ` `50` `]; ` ` ` ` ` `# Size of arr[] ` ` ` `n ` `=` `len` `(arr); ` ` ` ` ` `# Function that returns the minimum ` ` ` `# the absolute difference between ` ` ` `# median of partition array ` ` ` `print` `(minimiseMedian(arr, n)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to minimise the ` `// median between partition array ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find minimise the ` ` ` `// median between partition array ` ` ` `static` `int` `minimiseMedian(` `int` `[]arr, ` `int` `n) ` ` ` `{ ` ` ` `// Sort the given array []arr ` ` ` `Array.Sort(arr); ` ` ` ` ` `// Return the difference of two ` ` ` `// middle element of the []arr ` ` ` `return` `Math.Abs(arr[n / 2] - arr[(n / 2) - 1]); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{ ` ` ` `int` `[]arr = { 15, 25, 35, 50 }; ` ` ` ` ` `// Size of []arr ` ` ` `int` `n = arr.Length; ` ` ` ` ` `// Function that returns the minimum ` ` ` `// the absolute difference between ` ` ` `// median of partition array ` ` ` `Console.WriteLine(minimiseMedian(arr, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

10

**Time Complexity:** O(N*log N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Partition a set into two subsets such that difference between max of one and min of other is minimized
- Divide Matrix into K groups of adjacent cells having minimum difference between maximum and minimum sized groups
- Check if array can be divided into two sub-arrays such that their absolute difference is K
- Split first N natural numbers into two sets with minimum absolute difference of their sums
- Split squares of first N natural numbers into two sets with minimum absolute difference of their sums
- Delete odd and even numbers at alternate step such that sum of remaining elements is minimized
- Find K elements whose absolute difference with median of array is maximum
- Partition array into minimum number of equal length subsets consisting of a single distinct value
- Ways of dividing a group into two halves such that two elements are in different groups
- Number of ways of distributing N identical objects in R distinct groups with no groups empty
- Divide 1 to n into two groups with minimum sum difference
- Partition into two subarrays of lengths k and (N - k) such that the difference of sums is maximum
- Partition a set into two non-empty subsets such that the difference of subset sums is maximum
- Choose k array elements such that difference of maximum and minimum is minimized
- Divide a sorted array in K parts with sum of difference of max and min minimized in each part
- Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal
- Replace the odd positioned elements with their cubes and even positioned elements with their squares
- Ways to split array into two groups of same XOR value
- Partition N into M parts such that difference between Max and Min part is smallest
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.