# Program to find the Hidden Number

Given an array of integers, The task is to find another integer such that, if all the elements of the array are subtracted individually from the number , then the sum of all the differences should add to 0. If any such integer exists, print the value of , else print .

Example

```Input: arr[] = {1, 2, 3}
Output: 2
Explanation:
Substracting all the elements of arrays from 2,
The sum of difference is:
1 + 0 + (-1) = 0.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Solution: The idea is to calculate the total sum of the array and divide it by the size of the array. If the sum of the array is perfectly divisible by its size then the quotient obtained from this division operation will be the required hidden number.

Below is the implementation of the above idea:

## C++

 `// C++ Program to find the ` `// hidden number ` ` `  `#include ` `using` `namespace` `std; ` ` `  `    ``// Driver Code ` ` `  `int` `main() { ` `        ``// Getting the size of array ` `        ``int` `n = 3; ` ` `  `        ``// Getting the array of size n ` `        ``int` `a[] = { 1, 2, 3 }; ` ` `  `        ``// Solution ` `        ``int` `i = 0; ` ` `  `        ``// Finding sum of the array elements ` `        ``long` `sum = 0; ` `        ``for` `(i = 0; i < n; i++) { ` `            ``sum += a[i]; ` `        ``} ` ` `  `        ``// Dividing sum by size n ` `        ``long` `x = sum / n; ` ` `  `        ``// Print x, if found ` `        ``if` `(x * n == sum) ` `            ``cout<

## Java

 `// Java Program to find the ` `// hidden number ` ` `  `public` `class` `GFG { ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` ` `  `        ``// Getting the size of array ` `        ``int` `n = ``3``; ` ` `  `        ``// Getting the array of size n ` `        ``int` `a[] = { ``1``, ``2``, ``3` `}; ` ` `  `        ``// Solution ` `        ``int` `i = ``0``; ` ` `  `        ``// Finding sum of the array elements ` `        ``long` `sum = ``0``; ` `        ``for` `(i = ``0``; i < n; i++) { ` `            ``sum += a[i]; ` `        ``} ` ` `  `        ``// Dividing sum by size n ` `        ``long` `x = sum / n; ` ` `  `        ``// Print x, if found ` `        ``if` `(x * n == sum) ` `            ``System.out.println(x); ` `        ``else` `            ``System.out.println(``"-1"``); ` `    ``} ` `} `

## Python 3

 `# Python 3 Program to find the ` `# hidden number ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"``: ` `     `  `    ``# Getting the size of array ` `    ``n ``=` `3` ` `  `    ``# Getting the array of size n ` `    ``a ``=` `[ ``1``, ``2``, ``3` `] ` ` `  `    ``# Solution ` `    ``i ``=` `0` ` `  `    ``# Finding sum of the . ` `    ``# array elements ` `    ``sum` `=` `0` `    ``for` `i ``in` `range``(n): ` `        ``sum` `+``=` `a[i] ` ` `  `    ``# Dividing sum by size n ` `    ``x ``=` `sum` `/``/` `n ` ` `  `    ``# Print x, if found ` `    ``if` `(x ``*` `n ``=``=` `sum``): ` `        ``print``(x) ` `    ``else``: ` `        ``print``(``"-1"``) ` ` `  `# This code is contributed  ` `# by ChitraNayal `

## C#

 `// C# Program to find the ` `// hidden number ` `using` `System;  ` ` `  `class` `GFG ` `{  ` ` `  `// Driver Code  ` `public` `static` `void` `Main()  ` `{  ` ` `  `    ``// Getting the size of array  ` `    ``int` `n = 3;  ` ` `  `    ``// Getting the array of size n  ` `    ``int` `[]a = { 1, 2, 3 };  ` ` `  `    ``// Solution  ` `    ``int` `i = 0;  ` ` `  `    ``// Finding sum of the ` `    ``// array elements  ` `    ``long` `sum = 0;  ` `    ``for` `(i = 0; i < n; i++)  ` `    ``{  ` `        ``sum += a[i];  ` `    ``}  ` ` `  `    ``// Dividing sum by size n  ` `    ``long` `x = sum / n;  ` ` `  `    ``// Print x, if found  ` `    ``if` `(x * n == sum)  ` `        ``Console.WriteLine(x);  ` `    ``else` `        ``Console.WriteLine(``"-1"``);  ` `}  ` `}  ` ` `  `// This code is contributed ` `// by inder_verma `

## PHP

 ` `

Output:

```2
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.