Program to find the Hidden Number

25 May, 2022
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: 
Subtracting all the elements of arrays from 2,
The sum of difference is:
1 + 0 + (-1) = 0.
 
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++
Java
Python 3
C#
PHP
Javascript
// C++ Program to find the// hidden number #include <iostream>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<<x<<endl;        else            cout<<("-1")<<endl;          return 0;    // This code is contributed// by shashank}
// 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 Program to find the# hidden number # Driver Codeif __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# Program to find the// hidden numberusing System; class GFG{ // Driver Codepublic 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// PHP Program to find the// hidden number // Driver Code // Getting the size of array$n = 3; // Getting the array of size n$a = array( 1, 2, 3 ); // Solution$i = 0; // Finding sum of the array elements$sum = 0;for ($i = 0; $i < $n; $i++){    $sum += $a[$i];} // Dividing sum by size n$x = $sum / $n; // Print x, if foundif ($x * $n == $sum)echo($x);elseecho("-1"); // This code is contributed// by inder_verma?>
<script> // JavaScript Program to find the// hidden number         // Driver Code    // Getting the size of array    let n = 3;    // Getting the array of size n    let a=[1, 2, 3];         // Solution    let i = 0;         // Finding sum of the array elements    let sum = 0;    for (i = 0; i < n; i++) {            sum += a[i];        }           // Dividing sum by size n        let x = sum / n;           // Print x, if found        if (x * n == sum)            document.write(x);        else            document.write("-1");  // This code is contributed by rag2127 </script>
2
 
Time Complexity: O(n), where n is the length of the given array. Auxiliary space: O(1)
inderDuMCA
Shashank12
ukasp
april14
rag2127
sweetyty
shivamanandrj9
Constructive Algorithms
Technical Scripter 2018
Arrays
Mathematical
Technical Scripter
Arrays
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.