# Maximize the median of an array

Given an array of n elements. The task is to print the array in a form such that the median of that array is maximum.

Examples:

```Input: arr[] = {3, 1, 2, 3, 8}
Output: 3 1 8 2 3

Input: arr[] = {9, 8, 7, 6, 5, 4}
Output: 7 6 9 8 5 4
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Median of any sequence is the middle most element of given array. Hence if the array has an odd number of elements the n/2 th element is the median of the array and in the case, if the array has even number of elements, then n/2th and n/2 – 1 th element are median.

To maximize the median of any array, first of all, check whether its size is even or odd depending upon the size of array perform following steps.

1. If size is odd: Find the maximum element from array and swap it with the n/2th element.
2. If size is even: Find the first two maximum element and swap them with n/2th and n/2-1 th elements.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the above approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to print array with maximum median ` `void` `printMaxMedian(``int` `arr[], ``int` `n) ` `{ ` `    ``// case when size of array is odd ` `    ``if` `(n % 2) { ` `        ``int``* maxElement = max_element(arr, arr + n); ` `        ``swap(*maxElement, arr[n / 2]); ` `    ``} ` ` `  `    ``// when the size of the array is even ` `    ``else` `{ ` `        ``// find 1st maximum element ` `        ``int``* maxElement1 = max_element(arr, arr + n); ` ` `  `        ``// find 2nd maximum element ` `        ``int``* maxElement2 = max_element(arr, maxElement1); ` `        ``maxElement2 = max(maxElement2, ` `                          ``max_element(maxElement1 + 1, arr + n)); ` ` `  `        ``// swap position for median ` `        ``swap(*maxElement1, arr[n / 2]); ` `        ``swap(*maxElement2, arr[n / 2 - 1]); ` `    ``} ` ` `  `    ``// print resultant array ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``cout << arr[i] << ``" "``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 4, 8, 3, 1, 3, 7, 0, 4 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``printMaxMedian(arr, n); ` ` `  `    ``return` `0; ` `} `

## Python3

 `# Python3 implementation of the above approach  ` ` `  `# Function to print the array with  ` `# maximum median  ` `def` `printMaxMedian(arr, n):  ` ` `  `    ``# case when size of the array is odd  ` `    ``if` `n ``%` `2` `!``=` `0``: ` `        ``maxElement ``=` `arr.index(``max``(arr))  ` `        ``(arr[maxElement],  ` `         ``arr[n ``/``/` `2``]) ``=` `(arr[n ``/``/` `2``],  ` `                         ``arr[maxElement])  ` ` `  `    ``# when size of array is even  ` `    ``else``: ` `         `  `        ``# find 1st maximum element  ` `        ``maxElement1 ``=` `arr.index(``max``(arr))  ` ` `  `        ``# find 2nd maximum element  ` `        ``maxElement2 ``=` `arr.index(``max``(arr[``0` `: maxElement1]))  ` `        ``maxElement2 ``=` `arr.index(``max``(arr[maxElement2],  ` `                                ``max``(arr[maxElement1 ``+` `1``:]))) ` ` `  `        ``# swap position for median  ` `        ``(arr[maxElement1],  ` `         ``arr[n ``/``/` `2``]) ``=` `(arr[n ``/``/` `2``],  ` `                         ``arr[maxElement1])  ` `        ``(arr[maxElement2], ` `         ``arr[n ``/``/` `2` `-` `1``]) ``=` `(arr[n ``/``/` `2` `-` `1``],  ` `                             ``arr[maxElement2]) ` ` `  `    ``# print the resultant array  ` `    ``for` `i ``in` `range``(``0``, n):  ` `        ``print``(arr[i], end ``=` `" "``)  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"``:  ` ` `  `    ``arr ``=` `[``4``, ``8``, ``3``, ``1``, ``3``, ``7``, ``0``, ``4``]  ` `    ``n ``=` `len``(arr)  ` `    ``printMaxMedian(arr, n)  ` ` `  `# This code is contributed by Rituraj Jain `

Output:

```4 3 3 7 8 1 0 4
```

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.

My Personal Notes arrow_drop_up Discovering ways to develop a plane for soaring career goals

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.

Improved By : rituraj_jain

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.