Skip to content
Related Articles

Related Articles

Count of elements that can be deleted without disturbing the mean of the initial array
  • Last Updated : 06 May, 2021

Given an array arr[] of N integers, the task is to find the count of elements from the array such that after removing them individually (only a single element can be deleted) from the array will not disturb the initial mean of the array.
Examples: 
 

Input: arr[] = {1, 2, 3, 4, 5} 
Output:
3 is the only element removing which 
will not affect the mean of the array. 
i.e. (1 + 2 + 4 + 5) / 4 = 12 / 4 = 3 
which is the mean of the original array.
Input: arr[] = {5, 4, 3, 6} 
Output:
 

 

Approach: 
 

  • Find the initial mean and the sum of the array elements and store them in the variables mean and sum respectively.
  • Now, initialise count = 0 and for every element arr[i] if (sum – arr[i]) / (N – 1) = mean then increment the count.
  • Print the count in the end.

Below is the implementation of the above approach: 
 



C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Funcion to find the elements
// which do not change the mean on removal
int countElements(int arr[], int n)
{
 
    // To store the sum of the array elements
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum += arr[i];
 
    // To store the initial mean
    float mean = (float)sum / n;
 
    // to store the count of required elements
    int cnt = 0;
    // Iterate over the array
    for (int i = 0; i < n; i++) {
 
        // Finding the new mean
        float newMean = (float)(sum - arr[i]) / (n - 1);
 
        // If the new mean equals to the initial mean
        if (newMean == mean)
            cnt++;
    }
 
    return cnt;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << countElements(arr, n);
 
    return 0;
}

Java




// Java implementation of the approach
class GFG
{
 
// Funcion to find the elements
// which do not change the mean on removal
static int countElements(int arr[], int n)
{
 
    // To store the sum of the array elements
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum += arr[i];
 
    // To store the initial mean
    float mean = (float)sum / n;
 
    // to store the count of required elements
    int cnt = 0;
     
    // Iterate over the array
    for (int i = 0; i < n; i++)
    {
 
        // Finding the new mean
        float newMean = (float)(sum - arr[i]) /
                                      (n - 1);
 
        // If the new mean equals to
        // the initial mean
        if (newMean == mean)
            cnt++;
    }
    return cnt;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int n = arr.length;
    System.out.println(countElements(arr, n));
}
}
 
// This code is contributed by PrinciRaj1992

Python3




# Python3 implementation of the approach
 
# Funcion to find the elements
# which do not change the mean on removal
def countElements(arr, n):
 
    # To store the Sum of the array elements
    Sum = 0
    for i in range(n):
        Sum += arr[i]
 
    # To store the initial mean
    mean = Sum / n
 
    # to store the count of required elements
    cnt = 0
     
    # Iterate over the array
    for i in range(n):
         
        # Finding the new mean
        newMean = (Sum - arr[i]) / (n - 1)
 
        # If the new mean equals to
        # the initial mean
        if (newMean == mean):
            cnt += 1
 
    return cnt
 
# Driver code
arr = [1, 2, 3, 4, 5]
n = len(arr)
 
print(countElements(arr, n))
 
# This code is contributed by Mohit Kumar

C#




// C# implementation of the approach
using System;
class GFG
{
 
// Funcion to find the elements
// which do not change the mean on removal
static int countElements(int []arr, int n)
{
 
    // To store the sum of the array elements
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum += arr[i];
 
    // To store the initial mean
    float mean = (float)sum / n;
 
    // to store the count of required elements
    int cnt = 0;
     
    // Iterate over the array
    for (int i = 0; i < n; i++)
    {
 
        // Finding the new mean
        float newMean = (float)(sum - arr[i]) /
                                      (n - 1);
 
        // If the new mean equals to
        // the initial mean
        if (newMean == mean)
            cnt++;
    }
    return cnt;
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 1, 2, 3, 4, 5 };
    int n = arr.Length;
    Console.WriteLine(countElements(arr, n));
}
}
 
// This code is contributed by 29AjayKumar

Javascript




<script>
 
// Javascript implementation of the approach
 
// Funcion to find the elements
// which do not change the mean on removal
function countElements(arr, n)
{
 
    // To store the sum of the array elements
    let sum = 0;
    for (let i = 0; i < n; i++)
        sum += arr[i];
 
    // To store the initial mean
    let mean = sum / n;
 
    // to store the count of required elements
    let cnt = 0;
    // Iterate over the array
    for (let i = 0; i < n; i++) {
 
        // Finding the new mean
        let newMean = (sum - arr[i]) / (n - 1);
 
        // If the new mean equals to the initial mean
        if (newMean == mean)
            cnt++;
    }
 
    return cnt;
}
 
// Driver code
    let arr = [ 1, 2, 3, 4, 5 ];
    let n = arr.length;
 
    document.write(countElements(arr, n));
 
</script>
Output: 
1

 

Time Complexity: O(N)
 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :