Number of ways to remove elements to maximize arithmetic mean
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: 3Remove elements at indices:
{ 0, 1, 2 }
{ 0, 2, 3 }
{ 0, 2 }Input: arr[] = { 1, 2, 3 }
Output: 1
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:
- Zero elements removed. Number of ways = 1.
- One element removed. Number of ways = 4.
- Two elements removed. Number of ways = 6.
- 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 <bits/stdc++.h> #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
?>
3
Recommended Posts:
- Number of ways to calculate a target number using only array elements
- Number of ways an array can be filled with 0s and 1s such that no consecutive elements are 1
- Remove an element to maximize the GCD of the given array
- Ways to Remove Edges from a Complete Graph to make Odd Edges
- Maximize the number of subarrays with XOR as zero
- Maximize the product of four factors of a Number
- Maximize the number of segments of length p, q and r
- Maximize the number of sum pairs which are divisible by K
- Ways to multiply n elements with an associative operation
- Remove minimum elements from the array such that 2*min becomes more than max
- Remove elements from the array which appear more than k times
- Ways of dividing a group into two halves such that two elements are in different groups
- Maximize a number considering permutations with values smaller than limit
- Rearrange the string to maximize the number of palindromic substrings
- Find d to maximize the number of zeros in array c[] created as c[i] = d*a[i] + b[i]
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.



