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: 3
Remove 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 the 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++
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int mod = 1000000007;
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;
}
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;
}
int main()
{
int arr[] = { 1, 2, 1, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << numberOfWays(arr, n);
return 0;
}
|
Java
import java.util.Arrays;
class GFG
{
static int mod = 1000000007 ;
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;
}
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;
}
public static void main (String[] args)
{
int []arr = { 1 , 2 , 1 , 2 };
int n = arr.length;
System.out.println(numberOfWays(arr, n));
}
}
|
Python3
mod = 1000000007 ;
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;
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;
if __name__ = = "__main__" :
arr = [ 1 , 2 , 1 , 2 ];
n = len (arr) ;
print (numberOfWays(arr, n));
|
C#
using System;
using System.Linq;
class GFG
{
static int mod = 1000000007;
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;
}
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;
}
static void Main()
{
int []arr = { 1, 2, 1, 2 };
int n = arr.Length;
Console.WriteLine(numberOfWays(arr, n));
}
}
|
PHP
<?php
function power( $x , $y , $p )
{
$res = 1;
$x = $x % $p ;
while ( $y > 0)
{
if ( $y & 1)
$res = ( $res * $x ) % $p ;
$y = $y >> 1;
$x = ( $x * $x ) % $p ;
}
return $res ;
}
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 ;
}
$arr = array ( 1, 2, 1, 2 );
$n = 4;
echo numberOfWays( $arr , $n );
?>
|
Javascript
<script>
let mod = 1000000007;
function power(a, n)
{
if (n == 0)
return 1;
let p = power(a, parseInt(n / 2, 10)) % mod;
p = (p * p) % mod;
if ((n & 1)>0)
p = (p * a) % mod;
return p;
}
function numberOfWays(arr, n)
{
let max_count = 0;
let max_value = Number.MIN_VALUE;
for (let i = 0; i < n; i++)
{
max_value = Math.max(max_value, arr[i]);
}
for (let i = 0; i < n; i++)
{
if (arr[i] == max_value)
max_count++;
}
return (power(2, max_count) - 1 + mod) % mod;
}
let arr = [ 1, 2, 1, 2 ];
let n = arr.length;
document.write(numberOfWays(arr, n));
</script>
|
Time Complexity: O(n + log(max_count))
Auxiliary Space: O(1)
Last Updated :
27 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...