Dada una array a [ ] de enteros positivos de tamaño N. La tarea es eliminar un elemento de la array dada de modo que la media aritmética de la array permanezca igual que antes. Si es posible eliminar dicho número, imprima ese número. De lo contrario, imprima -1.
Ejemplos:
Entrada: a[] = {1, 2, 3, 4, 5}
Salida: 3
La media de la array dada es 3. Después de eliminar la
array del tercer elemento, se convierte en {1, 2, 4, 5} cuya media también es 3.
Entrada: a[] = {5, 4, 3, 6}
Salida: -1
Acercarse :
Un enfoque eficiente es encontrar la media de la array y verificar si la media está presente en la array dada o no. Solo podemos eliminar elementos cuyo valor sea igual a la media.
sea la media de la array original M , la suma de los elementos sea sum . Entonces suma = METRO * N . Después de eliminar M , la nueva media será ( (M * N ) – M) / (N – 1) = M , que permanece igual. Así que simplemente use cualquier enfoque de búsqueda e imprima el elemento.
A continuación se muestra la implementación del enfoque anterior:
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; }
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..
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
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..
Javascript
<script> // Javascript 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 function FindElement(a, n) { // Find sum of all elements let sum = 0; for (let i = 0; i < n; i++) sum = sum + a[i]; // If mean is an integer if (sum % n == 0) { let m = parseInt(sum / n); // Check if mean is present in // the array or not for (let i = 0; i < n; i++) if (a[i] == m) return m; } return -1; } // Driver code let a = [ 1, 2, 3, 4, 5 ]; let n = a.length; document.write(FindElement(a, n)); </script>
Producción:
3
Complejidad de tiempo : O(N)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por ManishKhetan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA