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: 1
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: 0
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++
#include <bits/stdc++.h>
using namespace std;
int countElements( int arr[], int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
sum += arr[i];
float mean = ( float )sum / n;
int cnt = 0;
for ( int i = 0; i < n; i++) {
float newMean = ( float )(sum - arr[i]) / (n - 1);
if (newMean == mean)
cnt++;
}
return cnt;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << countElements(arr, n);
return 0;
}
|
Java
class GFG
{
static int countElements( int arr[], int n)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
sum += arr[i];
float mean = ( float )sum / n;
int cnt = 0 ;
for ( int i = 0 ; i < n; i++)
{
float newMean = ( float )(sum - arr[i]) /
(n - 1 );
if (newMean == mean)
cnt++;
}
return cnt;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int n = arr.length;
System.out.println(countElements(arr, n));
}
}
|
Python3
def countElements(arr, n):
Sum = 0
for i in range (n):
Sum + = arr[i]
mean = Sum / n
cnt = 0
for i in range (n):
newMean = ( Sum - arr[i]) / (n - 1 )
if (newMean = = mean):
cnt + = 1
return cnt
arr = [ 1 , 2 , 3 , 4 , 5 ]
n = len (arr)
print (countElements(arr, n))
|
C#
using System;
class GFG
{
static int countElements( int []arr, int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
sum += arr[i];
float mean = ( float )sum / n;
int cnt = 0;
for ( int i = 0; i < n; i++)
{
float newMean = ( float )(sum - arr[i]) /
(n - 1);
if (newMean == mean)
cnt++;
}
return cnt;
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4, 5 };
int n = arr.Length;
Console.WriteLine(countElements(arr, n));
}
}
|
Javascript
<script>
function countElements(arr, n)
{
let sum = 0;
for (let i = 0; i < n; i++)
sum += arr[i];
let mean = sum / n;
let cnt = 0;
for (let i = 0; i < n; i++) {
let newMean = (sum - arr[i]) / (n - 1);
if (newMean == mean)
cnt++;
}
return cnt;
}
let arr = [ 1, 2, 3, 4, 5 ];
let n = arr.length;
document.write(countElements(arr, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
04 Jun, 2022
Like Article
Save Article