Dada una array de números enteros, la tarea es encontrar otro número entero tal que, si todos los elementos de la array se restan individualmente del número , entonces la suma de todas las diferencias debe sumar 0. Si existe algún número entero, imprima el valor de , de lo contrario imprimir .
Ejemplo
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.
Solución : la idea es calcular la suma total de la array y dividirla por el tamaño de la array. Si la suma de la array es perfectamente divisible por su tamaño, el cociente obtenido de esta operación de división será el número oculto requerido.
A continuación se muestra la implementación de la idea anterior:
C++
// 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
// 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
<?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 found if ($x * $n == $sum) echo($x); else echo("-1"); // This code is contributed // by inder_verma ?>
Javascript
<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
Complejidad de tiempo: O(n), donde n es la longitud de la array dada.
Espacio auxiliar : O(1)
Publicación traducida automáticamente
Artículo escrito por anonymousgroup y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA