# Number of ways to remove elements to maximize arithmetic mean

• Last Updated : 03 Jun, 2021

Given an array arr[], the task is to find the number of ways to remove elements from the array so as to maximize the arithmetic mean of the remaining array.
Examples:

Input: arr[] = { 1, 2, 1, 2 }
Output: 3
Remove elements at indices:
{ 0, 1, 2 }
{ 0, 2, 3 }
{ 0, 2 }
Input: arr[] = { 1, 2, 3 }
Output:

Approach: The arithmetic mean of the array is maximized when only the maximum element(s) remains in the array.
Now consider the array arr[] = { 3, 3, 3, 3 }
We just need to make sure that at least one instance of the maximum element remains in the array after removing the other elements. This will guarantee maximization of the arithmetic mean. Hence we need to remove at most 3 elements from the above array. The number of ways to remove at most 3 elements:

1. Zero elements removed. Number of ways = 1.
2. One element removed. Number of ways = 4.
3. Two elements removed. Number of ways = 6.
4. Three elements removed. Number of ways = 4.

Hence total = 1 + 4 + 6 + 4 = 15 = 24 – 1.
Now consider the array = { 1, 4, 3, 2, 3, 4, 4 }
On sorting the array becomes = { 1, 2, 3, 3, 4, 4, 4 }. In this case, there are elements other than 4. We can remove at most 2 instances of 4 and when those instances are removed, the other elements (which are not 4) should always be removed with them. Hence the number of ways will remain the same as the number of ways to remove at most 2 instances of 4.
The various ways of removing elements:
{ 1, 2, 3, 3 }
{ 1, 2, 3, 3, 4 }
{ 1, 2, 3, 3, 4 }
{ 1, 2, 3, 3, 4 }
{ 1, 2, 3, 3, 4, 4 }
{ 1, 2, 3, 3, 4, 4 }
{ 1, 2, 3, 3, 4, 4 }
Therefore the answer is 2count of max element – 1.
Below is the implementation of the above approach.

## C++

 `// C++ implementation of the above approach``#include ` `#define ll long long` `using` `namespace` `std;``const` `int` `mod = 1000000007;` `// Function to compute a^n``ll power(ll a, ll n)``{``    ``if` `(n == 0)``        ``return` `1;` `    ``ll p = power(a, n / 2) % mod;``    ``p = (p * p) % mod;``    ``if` `(n & 1)``        ``p = (p * a) % mod;` `    ``return` `p;``}` `// Function to return number of ways to maximize arithmetic mean``ll numberOfWays(``int``* arr, ``int` `n)``{` `    ``int` `max_count = 0;``    ``int` `max_value = *max_element(arr, arr + n);``    ``for` `(``int` `i = 0; i < n; i++) {``        ``if` `(arr[i] == max_value)``            ``max_count++;``    ``}``    ``return` `(power(2, max_count) - 1 + mod) % mod;``}` `// Driver code``int` `main()``{``    ``int` `arr[] = { 1, 2, 1, 2 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]);``    ``cout << numberOfWays(arr, n);``    ``return` `0;``}`

## Java

 `// Java implementation of the above approach``import` `java.util.Arrays;` `class` `GFG``{``    ` `static` `int` `mod = ``1000000007``;` `// Function to compute a^n``static` `int` `power(``int` `a, ``int` `n)``{``    ``if` `(n == ``0``)``        ``return` `1``;` `    ``int` `p = power(a, n / ``2``) % mod;``    ``p = (p * p) % mod;``    ``if` `((n & ``1``) > ``0``)``        ``p = (p * a) % mod;` `    ``return` `p;``}` `// Function to return number of``// ways to maximize arithmetic mean``static` `int` `numberOfWays(``int` `[]arr, ``int` `n)``{` `    ``int` `max_count = ``0``;``    ``int` `max_value = Arrays.stream(arr).max().getAsInt();``    ``for` `(``int` `i = ``0``; i < n; i++)``    ``{``        ``if` `(arr[i] == max_value)``            ``max_count++;``    ``}``    ``return` `(power(``2``, max_count) - ``1` `+ mod) % mod;``}` `// Driver code``public` `static` `void` `main (String[] args)``{``    ``int` `[]arr = { ``1``, ``2``, ``1``, ``2` `};``    ``int` `n = arr.length;``    ``System.out.println(numberOfWays(arr, n));``}``}` `// This code is contributed by mits`

## Python3

 `# Python3 implementation of the``# above approach` `mod ``=` `1000000007``;` `# Function to compute a^n``def` `power(a, n) :``    ` `    ``if` `(n ``=``=` `0``) :``        ``return` `1``;` `    ``p ``=` `power(a, n ``/``/` `2``) ``%` `mod;``    ``p ``=` `(p ``*` `p) ``%` `mod;``    ``if` `(n & ``1``) :``        ``p ``=` `(p ``*` `a) ``%` `mod;` `    ``return` `p;` `# Function to return number of ways``# to maximize arithmetic mean``def` `numberOfWays(arr, n) :` `    ``max_count ``=` `0``;``    ``max_value ``=` `max``(arr)``    ` `    ``for` `i ``in` `range``(n) :``        ``if` `(arr[i] ``=``=` `max_value) :``            ``max_count ``+``=` `1``;``            ` `    ``return` `(power(``2``, max_count) ``-` `1` `+` `mod) ``%` `mod;` `# Driver code``if` `__name__ ``=``=` `"__main__"` `:` `    ``arr ``=` `[ ``1``, ``2``, ``1``, ``2` `];``    ``n ``=` `len``(arr) ;``    ` `    ``print``(numberOfWays(arr, n));` `# This code is contributed by Ryuga`

## C#

 `// C# implementation of the above approach``using` `System;``using` `System.Linq;` `class` `GFG``{``    ` `static` `int` `mod = 1000000007;` `// Function to compute a^n``static` `int` `power(``int` `a, ``int` `n)``{``    ``if` `(n == 0)``        ``return` `1;` `    ``int` `p = power(a, n / 2) % mod;``    ``p = (p * p) % mod;``    ``if` `((n & 1)>0)``        ``p = (p * a) % mod;` `    ``return` `p;``}` `// Function to return number of``// ways to maximize arithmetic mean``static` `int` `numberOfWays(``int` `[]arr, ``int` `n)``{` `    ``int` `max_count = 0;``    ``int` `max_value = arr.Max();``    ``for` `(``int` `i = 0; i < n; i++)``    ``{``        ``if` `(arr[i] == max_value)``            ``max_count++;``    ``}``    ``return` `(power(2, max_count) - 1 + mod) % mod;``}` `// Driver code``static` `void` `Main()``{``    ``int` `[]arr = { 1, 2, 1, 2 };``    ``int` `n = arr.Length;``    ``Console.WriteLine(numberOfWays(arr, n));``}``}` `// This code is contributed by mits`

## PHP

 ` 0)``    ``{``        ``// If y is odd, multiply``        ``// x with result``        ``if` `(``\$y` `& 1)``            ``\$res` `= (``\$res` `* ``\$x``) % ``\$p``;` `        ``// y must be even now``        ` `        ``// y = \$y/2``        ``\$y` `= ``\$y` `>> 1;``        ``\$x` `= (``\$x` `* ``\$x``) % ``\$p``;``    ``}``    ``return` `\$res``;``}` `// Function to return number of ways``// to maximize arithmetic mean``function` `numberOfWays(``\$arr``, ``\$n``)``{``    ``\$mod` `= 1000000007;``    ``\$max_count` `= 0;``    ``\$max_value` `= ``\$arr``[0];``    ``for``(``\$i` `= 0; ``\$i` `< ``\$n``; ``\$i``++)``    ``if``(``\$max_value` `< ``\$arr``[``\$i``])``        ``\$max_value` `= ``\$arr``[``\$i``];``    ` `    ``for` `(``\$i` `= 0; ``\$i` `< ``\$n``; ``\$i``++)``    ``{``        ``if` `(``\$arr``[``\$i``] == ``\$max_value``)``            ``\$max_count``++;``    ``}``    ``return` `(power(2, ``\$max_count``,``                     ``\$mod``) - 1 + ``\$mod``) % ``\$mod``;``}` `// Driver code``\$arr` `= ``array``( 1, 2, 1, 2 );``\$n` `= 4;``echo` `numberOfWays(``\$arr``, ``\$n``);` `// This code is contributed``// by Arnab Kundu``?>`

## Javascript

 ``
Output:
`3`

My Personal Notes arrow_drop_up