📜  从数组中删除数字而不更改其算术平均值

📅  最后修改于: 2021-04-24 03:40:06             🧑  作者: Mango

给定大小为N的正整数数组a [] 。任务是从给定数组中删除一个元素,以使该数组的算术平均值与以前相同。如果可以删除任何这样的数字,请打印该数字。否则打印-1。

例子:

方法 :

一种有效的方法是找到数组的均值,并检查均值是否存在于给定的数组中。我们只能删除一个值等于均值的remove元素。

令原始数组的均值为M ,元素的和为sum 。然后求和= M * N。除去M后,新的平均值将为((M * N)– M)/(N – 1)= M ,保持不变。因此,只需使用任何搜索方法并打印元素即可。

下面是上述方法的实现:

C++
// CPP program to remove a number from the
// array without changing its arithmetic mean
#include 
using namespace std;
  
// Function to remove a number from the
// array without changing its arithmetic mean
int FindElement(int a[], int n)
{
    // Find sum of all elements
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum = sum + a[i];
  
    // If mean is an integer
    if (sum % n == 0) {
        int m = sum / n;
  
        // Check if mean is present in the array or not
        for (int i = 0; i < n; i++)
            if (a[i] == m)
                return m;
    }
  
    return -1;
}
  
// Driver code
int main()
{
    int a[] = { 1, 2, 3, 4, 5 };
  
    int n = sizeof(a) / sizeof(int);
  
    cout << FindElement(a, n);
  
    return 0;
}


Java
// Java program to remove a number from the
// array without changing its arithmetic mean
import java.io.*;
  
class GFG 
{
  
// Function to remove a number from the
// array without changing its arithmetic mean
static int FindElement(int a[], int n)
{
    // Find sum of all elements
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum = sum + a[i];
  
    // If mean is an integer
    if (sum % n == 0) 
    {
        int m = sum / n;
  
        // Check if mean is present in the array or not
        for (int i = 0; i < n; i++)
            if (a[i] == m)
                return m;
    }
  
    return -1;
}
  
// Driver code
public static void main (String[] args)
{
    int a[] = { 1, 2, 3, 4, 5 };
    int n = a.length;
  
    System.out.print(FindElement(a, n));
}
}
  
// This code is contributed by anuj_67..


Python3
# Python3 program to remove a number from the
# array without changing its arithmetic mean
  
# Function to remove a number from the
# array without changing its arithmetic mean
def FindElement(a, n):
  
    # Find sum of all elements
    s = 0
    for i in range(n):
        s = s + a[i]
  
    # If mean is an integer
    if s % n == 0:
        m = s // n
  
        # Check if mean is present
        # in the array or not
        for j in range(n):
            if a[j] == m:
                return m
    return -1
  
# Driver code
if __name__ == "__main__":
    a = [1, 2, 3, 4, 5]
    n = len(a)
    print(FindElement(a, n))
  
# This code is contributed by
# sanjeev2552


C#
// C# program to remove a number from the
// array without changing its arithmetic mean
using System;
  
class GFG 
{
  
// Function to remove a number from the
// array without changing its arithmetic mean
static int FindElement(int []a, int n)
{
    // Find sum of all elements
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum = sum + a[i];
  
    // If mean is an integer
    if (sum % n == 0) 
    {
        int m = sum / n;
  
        // Check if mean is present in the array or not
        for (int i = 0; i < n; i++)
            if (a[i] == m)
                return m;
    }
  
    return -1;
}
  
// Driver code
public static void Main ()
{
    int []a = { 1, 2, 3, 4, 5 };
    int n = a.Length;
  
    Console.WriteLine(FindElement(a, n));
}
}
  
// This code is contributed by anuj_67..


输出 :

3

时间复杂度: O(N)