Given an array **a[]** of positive integers of size **N**. The task is to remove one element from the given array such that the arithmetic mean of the array remains the same as it was before. If it is possible to remove any such number then print that number. Otherwise print -1.

**Examples:**

Input :a[] = {1, 2, 3, 4, 5}

Output :3Mean of the given array is 3. After emoving the 3rd element

array becomes {1, 2, 4, 5} whose mean is 3 too.

Input :a[] = {5, 4, 3, 6}

Output :-1

**Approach :**

An efficient approach is to find the mean of the array, and check whether the mean is present in the given array or not. We can only a remove element whose value is equal to mean.

let mean of the original array be **M**, the sum of the elements be **sum**. Then **sum = M * N**. After removing** M**, the new mean will be **( (M * N ) – M) / (N – 1) = M**, which remains same. So just use any search approach and print the element.

Below is the implementation of the above approach :

## C++

`// CPP program to remove a number from the ` `// array without changing its arithmetic mean ` `#include <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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.. ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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.. ` |

*chevron_right*

*filter_none*

**Output :**

3

**Time Complexity : **O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Number of steps to sort the array by changing order of three elements in each step
- Removing a number from array to make it Geometric Progression
- Find K such that changing all elements of the Array greater than K to K will make array sum N
- Minimize the number by changing at most K digits
- Sort an array without changing position of negative numbers
- Find the minimum number possible by changing at most one digit
- Divide array into increasing and decreasing subsequence without changing the order
- Check if a number is power of k using base changing method
- Find the largest number that can be formed by changing at most K digits
- Build Lowest Number by Removing n digits from a given number
- Empty an Array by removing maximum of K value from it
- Maximize the sum of sum of the Array by removing end elements
- Derive a MultiSet from given Array such that sum is > P and removing any element makes sum < P
- Count of ways to make Array sum even by removing only one element
- Min steps to empty an Array by removing a pair each time with sum at most K
- Steps to make array empty by removing maximum and its right side
- Minimize the sum calculated by repeatedly removing any two elements and inserting their sum to the Array
- Maximum length of Strictly Increasing Sub-array after removing at most one element
- Minimum number of distinct elements after removing M items | Set 2
- Minimum cost to make array size 1 by removing larger of pairs

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.