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

Output:4

The original array was {2, 7, 3, 4, 4, 4}

where 4 which occurred thrice was deleted.

(2 + 7 + 3 + 4 + 4 + 4) / 6 = 4

Input:arr[] = {5, 2, 3}, K = 4, avg = 7;

Output:-1

The required element is 9.75 which is not an integer.

**Approach:**

- Find the sum of the array elements and store it in a variable
**sum**. - Since
**X**appeared**K**times then the sum of the original array will be**sumOrg = sum + (X * K)**. - And the average is given to be
**avg**i.e.**avg = sumOrg / (N + K)**. - Now,
**X**can be easily calculated as**X = ((avg * (N + K)) – sum) / K**

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the missing element ` `int` `findMissing(` `int` `arr[], ` `int` `n, ` `int` `k, ` `int` `avg) ` `{ ` ` ` ` ` `// Find the sum of the array elements ` ` ` `int` `sum = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `sum += arr[i]; ` ` ` `} ` ` ` ` ` `// The numerator and the denominator ` ` ` `// of the equation ` ` ` `int` `num = (avg * (n + k)) - sum; ` ` ` `int` `den = k; ` ` ` ` ` `// If not divisible then X is ` ` ` `// not an integer ` ` ` `// it is a floating point number ` ` ` `if` `(num % den != 0) ` ` ` `return` `-1; ` ` ` ` ` `// Return X ` ` ` `return` `(num / den); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `k = 3, avg = 4; ` ` ` `int` `arr[] = { 2, 7, 3 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(` `int` `); ` ` ` ` ` `cout << findMissing(arr, n, k, avg); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the missing element ` ` ` `static` `int` `findMissing(` `int` `arr[], ` `int` `n, ` ` ` `int` `k, ` `int` `avg) ` ` ` `{ ` ` ` ` ` `// Find the sum of the array elements ` ` ` `int` `sum = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `sum += arr[i]; ` ` ` `} ` ` ` ` ` `// The numerator and the denominator ` ` ` `// of the equation ` ` ` `int` `num = (avg * (n + k)) - sum; ` ` ` `int` `den = k; ` ` ` ` ` `// If not divisible then X is ` ` ` `// not an integer ` ` ` `// it is a floating point number ` ` ` `if` `(num % den != ` `0` `) ` ` ` `return` `-` `1` `; ` ` ` ` ` `// Return X ` ` ` `return` `(` `int` `)(num / den); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `k = ` `3` `, avg = ` `4` `; ` ` ` `int` `arr[] = { ` `2` `, ` `7` `, ` `3` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `System.out.println(findMissing(arr, n, k, avg)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the missing element ` `def` `findMissing(arr, n, k, avg): ` ` ` ` ` `# Find the sum of the array elements ` ` ` `sum` `=` `0` `; ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `sum` `+` `=` `arr[i]; ` ` ` ` ` `# The numerator and the denominator ` ` ` `# of the equation ` ` ` `num ` `=` `(avg ` `*` `(n ` `+` `k)) ` `-` `sum` `; ` ` ` `den ` `=` `k; ` ` ` ` ` `# If not divisible then X is ` ` ` `# not an integer ` ` ` `# it is a floating ponumber ` ` ` `if` `(num ` `%` `den !` `=` `0` `): ` ` ` `return` `-` `1` `; ` ` ` ` ` `# Return X ` ` ` `return` `(` `int` `)(num ` `/` `den); ` ` ` `# Driver code ` `k ` `=` `3` `; avg ` `=` `4` `; ` `arr ` `=` `[` `2` `, ` `7` `, ` `3` `] ; ` `n ` `=` `len` `(arr); ` ` ` `print` `(findMissing(arr, n, k, avg)); ` ` ` `# This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the missing element ` ` ` `static` `int` `findMissing(` `int` `[]arr, ` `int` `n, ` ` ` `int` `k, ` `int` `avg) ` ` ` `{ ` ` ` ` ` `// Find the sum of the array elements ` ` ` `int` `sum = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `sum += arr[i]; ` ` ` `} ` ` ` ` ` `// The numerator and the denominator ` ` ` `// of the equation ` ` ` `int` `num = (avg * (n + k)) - sum; ` ` ` `int` `den = k; ` ` ` ` ` `// If not divisible then X is ` ` ` `// not an integer ` ` ` `// it is a floating point number ` ` ` `if` `(num % den != 0) ` ` ` `return` `-1; ` ` ` ` ` `// Return X ` ` ` `return` `(` `int` `)(num / den); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main (String[] args) ` ` ` `{ ` ` ` `int` `k = 3, avg = 4; ` ` ` `int` `[]arr = { 2, 7, 3 }; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `Console.WriteLine(findMissing(arr, n, k, avg)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** O(1)

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:

- Find original array from encrypted array (An array of sums of other elements)
- Count of elements that can be deleted without disturbing the mean of the initial array
- Smallest prefix to be deleted such that remaining array can be rearranged to form a sorted array
- Find the original Array using XOR values of all adjacent elements
- Print n smallest elements from given array in their original order
- Generate original array from an array that store the counts of greater elements on right
- Find kth smallest number in range [1, n] when all the odd numbers are deleted
- Generate array with minimum sum which can be deleted in P steps
- Average of remaining elements after removing K largest and K smallest elements from array
- Count subarrays having total distinct elements same as original array
- Generate original array from difference between every two consecutive elements
- Count occurrences of the average of array elements with a given number
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Find the original matrix when largest element in a row and a column are given
- Find the original coordinates whose Manhattan distances are given
- Sum of two numbers if the original ratio and new ratio obtained by adding a given number to each number is given
- Find original sequence from Array containing the sequence merged many times in order
- Minimum time to return array to its original state after given modifications
- Longest Sub-array with maximum average value
- Count number of permutation of an Array having no SubArray of size two or more from original Array

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.