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 medianvoid 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 codeint main(){ int arr[] = { 4, 8, 3, 1, 3, 7, 0, 4 }; int n = sizeof(arr) / sizeof(arr[0]); printMaxMedian(arr, n); return 0;} |
Java
// Java implementation of the above approach import java.util.*;import java.lang.*;import java.io.*;class GFG{public static int getIndexOfLargest(int[] array, int st, int n){ if (array == null || array.length == 0) // null or empty return -1; int largest = st; for(int i = st + 1; i < n; i++) { if (array[i] > array[largest]) largest = i; } // Position of the first largest // found return largest; }public static void swap(int a, int b, int[] arr){ int temp = arr[a]; arr[a] = arr[b]; arr[b] = temp;}// Function to print array with maximum medianpublic static void printMaxMedian(int[] arr, int n){ // Case when size of array is odd if (n % 2 != 0) { int maxElement = getIndexOfLargest( arr, 0, arr.length); swap(maxElement, n / 2, arr); } // When the size of the array is even else { // Find 1st maximum element int maxElement1 = getIndexOfLargest( arr, 0, arr.length); // Find 2nd maximum element int maxElement2 = getIndexOfLargest( arr, 0, maxElement1); maxElement2 = Math.max( arr[maxElement2], getIndexOfLargest(arr, maxElement1 + 1, arr.length)); // Swap position for median swap(maxElement1, n / 2, arr); swap(maxElement2, n / 2 - 1, arr); } // Print resultant array for(int i = 0; i < n; i++) System.out.print(arr[i] + " ");}// Driver Codepublic static void main(String[] args) throws java.lang.Exception{ int arr[] = { 4, 8, 3, 1, 3, 7, 0, 4 }; printMaxMedian(arr, arr.length);}}// This code is contributed by grand_master |
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 |
C#
// C# implementation of the above approach using System;class GFG{ static int getIndexOfLargest(int[] array, int st, int n){ if (array == null || array.Length == 0) // Null or empty return -1; int largest = st; for(int i = st + 1; i < n; i++) { if (array[i] > array[largest]) largest = i; } // Position of the first largest // found return largest; } static void swap(int a, int b, int[] arr){ int temp = arr[a]; arr[a] = arr[b]; arr[b] = temp;} // Function to print array with maximum medianstatic void printMaxMedian(int[] arr, int n){ // Case when size of array is odd if (n % 2 != 0) { int maxElement = getIndexOfLargest( arr, 0, arr.Length); swap(maxElement, n / 2, arr); } // When the size of the array is even else { // Find 1st maximum element int maxElement1 = getIndexOfLargest( arr, 0, arr.Length); // Find 2nd maximum element int maxElement2 = getIndexOfLargest( arr, 0, maxElement1); maxElement2 = Math.Max( arr[maxElement2], getIndexOfLargest(arr, maxElement1 + 1, arr.Length)); // Swap position for median swap(maxElement1, n / 2, arr); swap(maxElement2, n / 2 - 1, arr); } // Print resultant array for(int i = 0; i < n; i++) Console.Write(arr[i] + " ");}// Driver Codestatic void Main(){ int[] arr = { 4, 8, 3, 1, 3, 7, 0, 4 }; printMaxMedian(arr, arr.Length);}}// This code is contributed by divyeshrabadiya07 |
Javascript
function getIndexOfLargest(array, st, n){ if (array == null || array.length == 0) // null or empty return -1; let largest = st; for(let i = st + 1; i < n; i++) { if (array[i] > array[largest]) largest = i; } // Position of the first largest // found return largest; }function swap(a, b, arr){ let temp = arr[a]; arr[a] = arr[b]; arr[b] = temp;}// Function to print array with maximum medianfunction printMaxMedian(arr, n){ // Case when size of array is odd if (n % 2 != 0) { let maxElement = getIndexOfLargest(arr, 0, arr.length); swap(maxElement, n / 2, arr); } else { // Find 1st maximum element let maxElement1 = getIndexOfLargest(arr, 0, arr.length); // Find 2nd maximum element let maxElement2 = getIndexOfLargest(arr, 0, maxElement1); maxElement2 = Math.max(arr[maxElement2], getIndexOfLargest(arr, maxElement1 + 1,arr.length)); // Swap position for median swap(maxElement1, n / 2, arr); swap(maxElement2, n / 2 - 1, arr); } // Print resultant array for(let i = 0; i < n; i++){ document.write(arr[i] + " "); }} let arr = [ 4, 8, 3, 1, 3, 7, 0, 4 ]; printMaxMedian(arr, arr.length); // This code is contributed by aadityaburujwale. |
4 3 3 7 8 1 0 4
Complexity Analysis:
- Time Complexity: O(n), where n is the size of the given array
- Auxiliary Space: O(1), as no extra space is used
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
