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++ code for the above approach #include <bits/stdc++.h> using namespace std;
// Function to find the maximum number of // elements that can be deleted from the array int elementsDeleted(vector< int >& 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 Code int main()
{ vector< int > arr = { 3, 1, 1, 2, 4 };
cout << elementsDeleted(arr);
} |
// Java code for the above approach import java.util.*;
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 = 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 Code public static void main(String[] args)
{ int []arr = { 3 , 1 , 1 , 2 , 4 };
System.out.print(elementsDeleted(arr));
} } // This code is contributed by 29AjayKumar |
# Python code for the above approach # Function to find the maximum number of # elements that can be deleted from the array def 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 Code arr = [ 3 , 1 , 1 , 2 , 4 ]
print (elementsDeleted(arr))
# This code is contributed by Saurabh jaiswal |
// C# code for the above approach using 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. |
<script> // JavaScript code for the above approach
// Function to find the maximum number of
// elements that can be deleted from the array
function elementsDeleted(arr) {
// Size of the array
let N = arr.length;
// Minimum element
let it = Number.MAX_VALUE;
for (let i = 0; i < arr.length; i++) {
it = Math.min(it, arr[i]);
}
// Finding frequency of minimum element
let freq = 0;
for (let x of arr) {
if (x == it)
freq++;
}
return N - freq;
}
// Driver Code
let arr = [3, 1, 1, 2, 4];
document.write(elementsDeleted(arr));
// This code is contributed by Potta Lokesh </script>
|
3
Time Complexity: O(N)
Auxiliary Space: O(1)