Maximise count of elements that are strictly greater in a subsequence than their average
Last Updated :
16 Dec, 2021
Given an array arr[] of size N containing positive integers, the task is to find the maximum number of elements that can be deleted from the array using any number of operations. In one operation, select a subsequence from the given array, take their average and delete the numbers which are strictly greater than that average from the array.
Example:
Input: arr[] = {1, 1, 3, 2, 4}
Output: 3
Explanation:
Operation 1: Choose the subsequence {1, 2, 4}, average = (1+2+4)/3 = 2. So arr[5]=4 is deleted. arr[]={1, 1, 3, 2}
Operation 2: Choose the subsequence {1, 3, 2}, average = (1+3+2)/3 = 2. So arr[2]=3 is deleted. arr[]={1, 1, 2}
Operation 3: Choose the subsequence {1, 1}, average = (1+1)/2 = 1. So arr[3]=2 is deleted. arr[]={1, 1}
No further deletions can be performed.
Input: arr[] = {5, 5, 5}
Output: 0
Approach: The catch in this problem is that all elements except the minimum one can be deleted from the array because if only the minimum element is used to create the subsequence, then its average is basically the same element and all the other elements can be deleted. Now to solve this problem, follow the below steps:
- Find the frequency of minimum element, say freq.
- Return N-freq as the answer to this problem.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int elementsDeleted(vector< int >& arr)
{
int N = arr.size();
auto it = *min_element(arr.begin(), arr.end());
int freq = 0;
for ( auto x : arr) {
if (x == it)
freq++;
}
return N - freq;
}
int main()
{
vector< int > arr = { 3, 1, 1, 2, 4 };
cout << elementsDeleted(arr);
}
|
Java
import java.util.*;
class GFG{
static int elementsDeleted( int []arr)
{
int N = arr.length;
int it = Arrays.stream(arr).min().getAsInt();
int freq = 0 ;
for ( int x : arr) {
if (x == it)
freq++;
}
return N - freq;
}
public static void main(String[] args)
{
int []arr = { 3 , 1 , 1 , 2 , 4 };
System.out.print(elementsDeleted(arr));
}
}
|
Python3
def elementsDeleted(arr):
N = len (arr)
it = 10 * * 9
for i in range ( len (arr)):
it = min (it, arr[i])
freq = 0
for x in arr:
if (x = = it):
freq + = 1
return N - freq
arr = [ 3 , 1 , 1 , 2 , 4 ]
print (elementsDeleted(arr))
|
C#
using System;
using System.Linq;
class GFG {
static int elementsDeleted( int [] arr)
{
int N = arr.Length;
int it = arr.Min();
int freq = 0;
foreach ( int x in arr)
{
if (x == it)
freq++;
}
return N - freq;
}
public static void Main( string [] args)
{
int [] arr = { 3, 1, 1, 2, 4 };
Console.WriteLine(elementsDeleted(arr));
}
}
|
Javascript
<script>
function elementsDeleted(arr) {
let N = arr.length;
let it = Number.MAX_VALUE;
for (let i = 0; i < arr.length; i++) {
it = Math.min(it, arr[i]);
}
let freq = 0;
for (let x of arr) {
if (x == it)
freq++;
}
return N - freq;
}
let arr = [3, 1, 1, 2, 4];
document.write(elementsDeleted(arr));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...