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
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.
- If size is odd: Find the maximum element from array and swap it with the n/2th element.
- 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 <bits/stdc++.h> 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[0]); 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 |
4 3 3 7 8 1 0 4
Recommended Posts:
- Maximize the median of the given array after adding K elements to the same array
- Maximize the size of array by deleting exactly k sub-arrays to make array prime
- Rearrange the array to maximize the number of primes in prefix sum of the array
- Program for Mean and median of an unsorted array
- Maximize value of (arr[i] - i) - (arr[j] - j) in an array
- Maximize value of (a[i]+i)*(a[j]+j) in an array
- Given an array and three numbers, maximize (x * a[i]) + (y * a[j]) + (z * a[k])
- Maximize array sum after K negations | Set 2
- Maximize the bitwise OR of an array
- Maximize array sum after K negations | Set 1
- Maximize elements using another array
- Rearrange an array to maximize i*arr[i]
- Remove an element to maximize the GCD of the given array
- Maximize sum of consecutive differences in a circular array
- Remove array end element to maximize the sum of product
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



