Get the best out of our app
GeeksforGeeks App
Open App
Browser
Continue

# Maximise count of elements that are strictly greater in a subsequence than their average

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:

1. Find the frequency of minimum element, say freq.
2. Return N-freq as the answer to this problem.

Below is the implementation of the above approach:

## C++

 // C++ code for the above approach #include using namespace std; // Function to find the maximum number of// elements that can be deleted from the arrayint elementsDeleted(vector& arr){     // Size of the array    int N = arr.size();     // Minimum element    auto it = *min_element(arr.begin(), arr.end());     // Finding frequency of minimum element    int freq = 0;    for (auto x : arr) {        if (x == it)            freq++;    }     return N - freq;} // Driver Codeint main(){    vector arr = { 3, 1, 1, 2, 4 };    cout << elementsDeleted(arr);}

## Java

 // Java code for the above approachimport java.util.*; class GFG{ // Function to find the maximum number of// elements that can be deleted from the arraystatic int elementsDeleted(int []arr){     // Size of the array    int N = arr.length;     // Minimum element    int it = Arrays.stream(arr).min().getAsInt();     // Finding frequency of minimum element    int freq = 0;    for (int x : arr) {        if (x == it)            freq++;    }     return N - freq;} // Driver Codepublic static void main(String[] args){    int []arr = { 3, 1, 1, 2, 4 };    System.out.print(elementsDeleted(arr));}} // This code is contributed by 29AjayKumar

## Python3

 # Python code for the above approach # Function to find the maximum number of# elements that can be deleted from the arraydef elementsDeleted(arr):     # Size of the array    N = len(arr)     # Minimum element    it = 10**9     for i in range(len(arr)):        it = min(it, arr[i])     # Finding frequency of minimum element    freq = 0    for x in arr:        if (x == it):            freq += 1    return N - freq # Driver Codearr = [3, 1, 1, 2, 4]print(elementsDeleted(arr)) # This code is contributed by Saurabh jaiswal

## C#

 // C# code for the above approachusing System;using System.Linq;class GFG {     // Function to find the maximum number of    // elements that can be deleted from the array    static int elementsDeleted(int[] arr)    {         // Size of the array        int N = arr.Length;         // Minimum element        int it = arr.Min();         // Finding frequency of minimum element        int freq = 0;        foreach(int x in arr)        {            if (x == it)                freq++;        }         return N - freq;    }     // Driver Code    public static void Main(string[] args)    {        int[] arr = { 3, 1, 1, 2, 4 };        Console.WriteLine(elementsDeleted(arr));    }} // This code is contributed by ukasp.

## Javascript



Output

3

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up