Choose n elements such that their mean is maximum
Given an array of 2 * n elements, the task is to construct and print an array of n element such that the mean of the new array is maximum. Here n is even.
Examples:
Input: arr[] = {3, 1, 2, 3, 8, 6}
Output: 3 6 8
Input: arr[] = {3, 2, 3, 8}
Output: 3 8
Approach: Mean of an array is the average of the elements of the same array i.e (∑arr[i]) / n. So, in order for the mean of the array to be maximum, choose the maximum n elements from the array, this can be done by first sorting the array and then choosing the elements starting from the maximum.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Utility function to print the contents // of an array void printArray(int arr[], int n) { for (int i = 0; i < n; i++) cout << arr[i] << " "; } // Function to print the array with // maximum mean void printMaxMean(int arr[], int n) { int newArr[n]; // Sort the original array sort(arr, arr + 2 * n); // Construct new array for (int i = 0; i < n; i++) newArr[i] = arr[i + n]; // Print the resultant array printArray(newArr, n); } // Driver code int main() { int arr[] = { 4, 8, 3, 1, 3, 7, 0, 4 }; int n = sizeof(arr) / sizeof(arr[0]); printMaxMean(arr, n / 2); return 0; } |
Java
// Java implementation of the approach import java.util.Arrays; class GfG{ // Utility function to print the // contents of an array static void printArray(int arr[], int n) { for (int i = 0; i < n; i++) System.out.print(arr[i] + " "); } // Function to print the array // with maximum mean static void printMaxMean(int arr[], int n) { int newArr[] = new int[n]; // Sort the original array Arrays.sort(arr, 0, 2 * n); // Construct new array for (int i = 0; i < n; i++) newArr[i] = arr[i + n]; // Print the resultant array printArray(newArr, n); } // Driver code public static void main(String []args) { int arr[] = { 4, 8, 3, 1, 3, 7, 0, 4 }; int n = arr.length; printMaxMean(arr, n / 2); } } // This code is contributed by // Rituraj Jain |
Python3
# Python3 implementation of the approach # Utility function to print the contents # of an array def printArray(arr, n) : for i in range(n) : print(arr[i], end = " ") # Function to print the array with # maximum mean def printMaxMean(arr, n) : newArr = [0] * n # Sort the original array arr.sort() # Construct new array for i in range(n) : newArr[i] = arr[i + n] # Print the resultant array printArray(newArr, n) # Driver code if __name__ == "__main__" : arr = [ 4, 8, 3, 1, 3, 7, 0, 4 ] n = len(arr) printMaxMean(arr, n // 2) # This code is contributed by Ryuga |
C#
// C# implementation of the approach using System; class GfG { // Utility function to print the // contents of an array static void printArray(int[] arr, int n) { for (int i = 0; i < n; i++) Console.Write(arr[i] + " "); } // Function to print the array // with maximum mean static void printMaxMean(int[] arr, int n) { int[] newArr = new int[n]; // Sort the original array Array.Sort(arr, 0, 2 * n); // Construct new array for (int i = 0; i < n; i++) newArr[i] = arr[i + n]; // Print the resultant array printArray(newArr, n); } // Driver code public static void Main() { int[] arr = { 4, 8, 3, 1, 3, 7, 0, 4 }; int n = arr.Length; printMaxMean(arr, n / 2); } } // This code is contributed by Ita_c. |
PHP
<?php // PHP implementation of the approach // Utility function to print the contents // of an array function printArray($arr, $n) { for ($i = 0; $i < $n; $i++) echo $arr[$i] . " "; } // Function to print the array with // maximum mean function printMaxMean($arr, $n) { $newArr[$n] = array(); // Sort the original array sort($arr,0); // Construct new array for ($i = 0; $i < $n; $i++) $newArr[$i] = $arr[$i + $n]; // Print the resultant array printArray($newArr, $n); } // Driver code $arr = array(4, 8, 3, 1, 3, 7, 0, 4); $n = sizeof($arr); printMaxMean($arr, $n / 2); // This code is contributed // by Akanksha Rai |
4 4 7 8
Recommended Posts:
- Choose k array elements such that difference of maximum and minimum is minimized
- Choose X elements from A[] and Y elements from B[] which satisfy the given condition
- Choose two elements from the given array such that their sum is not present in any of the arrays
- Number of ways to choose elements from the array such that their average is K
- Number of ways to choose an integer such that there are exactly K elements greater than it in the given array
- Find all unique pairs of maximum and second maximum elements over all sub-arrays in O(NlogN)
- Maximum distinct elements after removing k elements
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Maximum value of |arr[0] - arr[1]| + |arr[1] - arr[2]| + ... +|arr[n - 2] - arr[n - 1]| when elements are from 1 to n
- Maximum possible elements which are divisible by 2
- Maximum in array which is at-least twice of other elements
- Sum of maximum elements of all subsets
- Maximum sum such that no two elements are adjacent
- Maximum value K such that array has at-least K elements that are >= K
- Maximum sum such that no two elements are adjacent | Set 2
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.


