Open In App

Count of elements that can be deleted without disturbing the mean of the initial array

Last Updated : 04 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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;
 
// Function 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
{
 
// Function 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
 
# Function 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
{
 
// Function 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
 
// Function 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)

Auxiliary Space: O(1)
 



Similar Reads

Maximize count of replaceable elements that can change initial GCD of Array
Given an integer array A[] of length N (N &gt;= 1), the task is to print the total number of indices in the array that can be used to modify the GCD of the whole array just by replacing elements at that index with any other positive integer. Examples: Input: arr[] = {5, 10, 20}Output: 3Explanation : All the three indices can be replaced with 1 to a
16 min read
Find Harmonic mean using Arithmetic mean and Geometric mean
Given two numbers, first calculate arithmetic mean and geometric mean of these two numbers. Using the arithmetic mean and geometric mean so calculated, find the harmonic mean between the two numbers. Examples: Input : a = 2 b = 4 Output : 2.666 Input : a = 5 b = 15 Output : 7.500 Arithmetic Mean: Arithmetic Mean 'AM' between two numbers a and b is
5 min read
Smallest prefix to be deleted such that remaining array can be rearranged to form a sorted array
Given an array arr[] consisting of N integers, the task is to find the minimum length of the prefix needed to be deleted such that the remaining array elements can be rearranged repeatedly by selecting the first or the last element one by one to form a sorted array. Examples: Input: arr[] = {6, 5, 4, 3, 4}Output: 3Explanation:To make an array sorte
5 min read
Count of triplets that can be removed without changing Mean of given Array
Given an array arr[], the task is to calculate the count of possible triplets such that they can be removed from the array without changing the arithmetic mean of the array. Example: Input: arr[] = {8, 7, 4, 6, 3, 0, 7}Output: 3Explanation: The given array has 3 possible triplets such that removing them will not affect the arithmetic mean of the ar
8 min read
Count of pairs that can be removed from Array without changing the Mean
Given an array arr[] of size N, the task is to find the number of pairs of the array upon removing which the mean (i.e. the average of all elements) of the array remains unchanged. Examples: Input: N = 5, arr[] = {1, 4, 7, 3, 5}Output: 2Explanation: The required pairs of positions are - {0, 2} and {3, 4}.Mean of original array = (1 + 4 + 7 + 3 + 5)
8 min read
Check if given Array can be formed from initial Array having one element
Given array A[], the task for this problem is to check whether A[] can be formed from array B[] = {1} by performing the following operation any number of times, select a subsequence of B[] and insert total sum of this subsequence in B[] to any position. Print "YES" if it is possible "NO otherwise. Examples: Input: A[] = {5, 1, 3, 2, 1}Output: YESEx
7 min read
Generate array with minimum sum which can be deleted in P steps
Given two numbers N and P. The task is to generate an array of all positive elements, and in one operation you can choose a minimum number in the array and subtract it from all array elements. If the array element becomes 0 then you will remove it. You have to print the minimum possible sum of the array and one possible array such that after applyi
6 min read
Maximize count of planes that can be stopped per second with help of given initial position and speed
Given two arrays A[] and B[] consisting of N integers where A[i] represents the initial position of the ith plane and B[i] is the speed at which the plane is landing, the task is to print the number of the plane that can be stopped from landing by shooting an aircraft at every second. Examples: Input: A[] = {1, 3, 5, 4, 8}, B[] = {1, 2, 2, 1, 2}Out
6 min read
Why can't a const variable be used to define an Array's initial size in C?
What is an array? An array is a collection of items of the same data type stored at contiguous memory locations. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array). The base value is index
3 min read
Find the deleted value from the array when average of original elements is given
Given an array of length N + K. Also given the average avg of all the elements of the array. If an element that appears exactly K time got removed from the array (all the occurrences) and the resultant array is given, the task is to find the element X. Note that if X is not an integer then print -1.Examples: Input: arr[] = {2, 7, 3}, K = 3, avg = 4
5 min read
Article Tags :
Practice Tags :