给定一个由N个整数组成的数组arr [] ,任务是从数组中查找元素的数量,以使得从数组中单独删除它们(仅可以删除单个元素)后,不会干扰数组的初始均值。
例子:
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
方法:
- 查找数组元素的初始均值和总和,并将它们分别存储在变量均值和总和中。
- 现在,初始化计数= 0,并且对于每个元素arr [i]如果(sum – arr [i])/(N – 1)=均值,则递增计数。
- 最后打印计数。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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
输出:
1
时间复杂度: O(N)