Choose n elements such that their mean is maximum
Last Updated :
29 Mar, 2023
Given an array of 2 * n elements, the task is to construct and print an array of n elements 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++
#include <bits/stdc++.h>
using namespace std;
void printArray( int arr[], int n)
{
for ( int i = 0; i < n; i++)
cout << arr[i] << " " ;
}
void printMaxMean( int arr[], int n)
{
int newArr[n];
sort(arr, arr + 2 * n);
for ( int i = 0; i < n; i++)
newArr[i] = arr[i + n];
printArray(newArr, n);
}
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
import java.util.Arrays;
class GfG{
static void printArray( int arr[], int n)
{
for ( int i = 0 ; i < n; i++)
System.out.print(arr[i] + " " );
}
static void printMaxMean( int arr[], int n)
{
int newArr[] = new int [n];
Arrays.sort(arr, 0 , 2 * n);
for ( int i = 0 ; i < n; i++)
newArr[i] = arr[i + n];
printArray(newArr, n);
}
public static void main(String []args)
{
int arr[] = { 4 , 8 , 3 , 1 , 3 , 7 , 0 , 4 };
int n = arr.length;
printMaxMean(arr, n / 2 );
}
}
|
Python3
def printArray(arr, n) :
for i in range (n) :
print (arr[i], end = " " )
def printMaxMean(arr, n) :
newArr = [ 0 ] * n
arr.sort()
for i in range (n) :
newArr[i] = arr[i + n]
printArray(newArr, n)
if __name__ = = "__main__" :
arr = [ 4 , 8 , 3 , 1 , 3 , 7 , 0 , 4 ]
n = len (arr)
printMaxMean(arr, n / / 2 )
|
C#
using System;
class GfG
{
static void printArray( int [] arr, int n)
{
for ( int i = 0; i < n; i++)
Console.Write(arr[i] + " " );
}
static void printMaxMean( int [] arr, int n)
{
int [] newArr = new int [n];
Array.Sort(arr, 0, 2 * n);
for ( int i = 0; i < n; i++)
newArr[i] = arr[i + n];
printArray(newArr, n);
}
public static void Main()
{
int [] arr = { 4, 8, 3, 1, 3, 7, 0, 4 };
int n = arr.Length;
printMaxMean(arr, n / 2);
}
}
|
PHP
<?php
function printArray( $arr , $n )
{
for ( $i = 0; $i < $n ; $i ++)
echo $arr [ $i ] . " " ;
}
function printMaxMean( $arr , $n )
{
$newArr [ $n ] = array ();
sort( $arr ,0);
for ( $i = 0; $i < $n ; $i ++)
$newArr [ $i ] = $arr [ $i + $n ];
printArray( $newArr , $n );
}
$arr = array (4, 8, 3, 1, 3, 7, 0, 4);
$n = sizeof( $arr );
printMaxMean( $arr , $n / 2);
|
Javascript
<script>
function printArray(arr , n) {
for (i = 0; i < n; i++)
document.write(arr[i] + " " );
}
function printMaxMean(arr , n) {
var newArr = Array(n).fill(0);
arr.sort((a,b)=>a-b);
for (i = 0; i < n; i++)
newArr[i] = arr[i + n];
printArray(newArr, n);
}
var arr = [ 4, 8, 3, 1, 3, 7, 0, 4 ];
var n = arr.length;
printMaxMean(arr, n / 2);
</script>
|
Complexity Analysis:
- Time Complexity: O(n logn)
- Auxiliary Space: O(1)
Another Efficient Approach using the MAP STL:
In this approach we will using the map data structure because map itself stores the elements in increasing order or sorted order. So, first we will be storing the array elements in the map and then will print the elements from the last to get the maximum mean.
Implementation of Above Approach is given below:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > findNelements(vector< int > vec, int n){
map< int , int > count;
for ( int i = 0; i < 2*n; i++){
count[vec[i]]++;
}
vector< int > result;
int last = count.rbegin()->first;
while (n > 0){
if (count[last] == 0) last--;
else {
result.push_back(last);
count[last]--;
--n;
}
}
return result;
}
void printElements(vector< int > output){
for ( int i = output.size()-1; i >= 0; i--){
cout << output[i] << " " ;
}
}
int main() {
vector< int > vec = { 4, 8, 3, 1, 3, 7, 0, 4 };
int n = vec.size()/2;
vector< int > output = findNelements(vec, n);
printElements(output);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static List<Integer>
findNelements(List<Integer> vec, int n)
{
Map<Integer, Integer> count = new HashMap<>();
for ( int i = 0 ; i < vec.size(); i++)
{
count.put(vec.get(i),
count.getOrDefault(vec.get(i), 0 )
+ 1 );
}
List<Integer> result = new ArrayList<>();
int last = Collections.max(count.keySet());
while (n > 0 ) {
if (count.get(last) == null
|| count.get(last) == 0 )
last--;
else {
result.add(last);
count.put(last, count.get(last) - 1 );
n--;
}
}
return result;
}
public static void printElements(List<Integer> output)
{
for ( int i = output.size() - 1 ; i >= 0 ; i--) {
System.out.print(output.get(i) + " " );
}
}
public static void main(String[] args)
{
List<Integer> vec
= Arrays.asList( 4 , 8 , 3 , 1 , 3 , 7 , 0 , 4 );
int n = vec.size() / 2 ;
List<Integer> output = findNelements(vec, n);
printElements(output);
}
}
|
Python3
from collections import defaultdict
def findNelements(vec, n):
count = defaultdict( int )
for i in range ( 2 * n):
count[vec[i]] + = 1
result = []
last = max (count.keys())
while n > 0 :
if count[last] = = 0 :
last - = 1
else :
result.append(last)
count[last] - = 1
n - = 1
return result
def printElements(output):
for i in range ( len (output) - 1 , - 1 , - 1 ):
print (output[i], end = ' ' )
print ()
if __name__ = = '__main__' :
vec = [ 4 , 8 , 3 , 1 , 3 , 7 , 0 , 4 ]
n = len (vec) / / 2
output = findNelements(vec, n)
printElements(output)
|
Javascript
function findNelements(vec, n) {
let count = new Map();
for (let i = 0; i < 2*n; i++) {
count.set(vec[i], (count.get(vec[i]) || 0) + 1);
}
let result = [];
let keys = Array.from(count.keys()).sort((a, b) => b - a);
for (let i = 0; i < keys.length && n > 0; i++) {
let key = keys[i];
let freq = count.get(key);
while (freq > 0 && n > 0) {
result.push(key);
freq--;
n--;
}
}
return result;
}
function printElements(output) {
for (let i = output.length - 1; i >= 0; i--) {
process.stdout.write(output[i] + ' ' );
}
console.log();
}
let vec = [4, 8, 3, 1, 3, 7, 0, 4];
let n = vec.length / 2;
let output = findNelements(vec, n);
printElements(output);
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
public static List< int > FindNelements(List< int > vec,
int n)
{
Dictionary< int , int > count
= new Dictionary< int , int >();
for ( int i = 0; i < vec.Count; i++) {
if (count.ContainsKey(vec[i])) {
count[vec[i]]++;
}
else {
count[vec[i]] = 1;
}
}
List< int > result = new List< int >();
int last = count.Keys.Max();
while (n > 0) {
if (!count.ContainsKey(last)
|| count[last] == 0) {
last--;
}
else {
result.Add(last);
count[last]--;
n--;
}
}
return result;
}
public static void PrintElements(List< int > output)
{
for ( int i = output.Count - 1; i >= 0; i--) {
Console.Write(output[i] + " " );
}
}
public static void Main( string [] args)
{
List< int > vec
= new List< int >{ 4, 8, 3, 1, 3, 7, 0, 4 };
int n = vec.Count / 2;
List< int > output = FindNelements(vec, n);
PrintElements(output);
}
}
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...