Dada una array arr[] , la tarea es verificar si es posible hacer que todos los elementos de la array sean cero mediante la operación dada. En una sola operación, dos elementos cualesquiera arr[i] y arr[j] pueden reducirse en uno al mismo tiempo.
Ejemplos:
Entrada: arr[] = {1, 2, 1, 2, 2}
Salida: Sí
Decrementa el 1 er y el 2 do elemento, arr[] = {0, 1, 1, 2, 2}
Decrementa el 2 do y el 3er elemento , arr[] = {0, 0, 0, 2, 2}
Decrementa el 4 to y el 5 to elemento, arr[] = {0, 0, 0, 1, 1}
Decrementa el 4 to y el 5 to elemento, arr[] = {0, 0, 0, 0, 0}
Entrada: arr[] = {1, 2, 3, 4, 5}
Salida: No
Enfoque: la array dada solo se puede convertir en cero si cumple las siguientes condiciones:
- La suma de los elementos del arreglo debe ser par.
- Los elementos más grandes deben ser menores o iguales a ⌊sum / 2⌋ donde sum es la suma de los otros elementos.
A continuación se muestra la implementación del enfoque anterior:
CPP
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that returns true if all // the array elements can be made // 0 with the given operation bool checkZeroArray(int* arr, int n) { // Find the maximum element // and the sum int sum = 0, maximum = INT_MIN; for (int i = 0; i < n; i++) { sum = sum + arr[i]; maximum = max(maximum, arr[i]); } // Check the required condition if (sum % 2 == 0 && maximum <= sum / 2) return true; return false; } // Driver code int main() { int arr[] = { 1, 2, 1, 2, 2 }; int n = sizeof(arr) / sizeof(int); if (checkZeroArray(arr, n)) cout << "Yes"; else cout << "No"; return 0; }
Java
// Java implementation of the above approach public class GFG { // Function that returns true if all // the array elements can be made // 0 with the given operation static boolean checkZeroArray(int []arr, int n) { // Find the maximum element // and the sum int sum = 0, maximum = Integer.MIN_VALUE; for (int i = 0; i < n; i++) { sum = sum + arr[i]; maximum = Math.max(maximum, arr[i]); } // Check the required condition if (sum % 2 == 0 && maximum <= sum / 2) return true; return false; } // Driver code public static void main (String[] args) { int arr[] = { 1, 2, 1, 2, 2 }; int n = arr.length; if (checkZeroArray(arr, n) == true) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by AnkitRai01
Python
# Python3 implementation of the approach # Function that returns true if all # the array elements can be made # 0 with the given operation def checkZeroArray(arr,n): # Find the maximum element # and the sum sum = 0 maximum = -10**9 for i in range(n): sum = sum + arr[i] maximum = max(maximum, arr[i]) # Check the required condition if (sum % 2 == 0 and maximum <= sum // 2): return True return False # Driver code arr = [1, 2, 1, 2, 2] n = len(arr) if (checkZeroArray(arr, n)): print("Yes") else: print("No") # This code is contributed by mohit kumar 29
C#
// C# implementation of the above approach using System; class GFG { // Function that returns true if all // the array elements can be made // 0 with the given operation static bool checkZeroArray(int []arr, int n) { // Find the maximum element // and the sum int sum = 0, maximum = int.MinValue; for (int i = 0; i < n; i++) { sum = sum + arr[i]; maximum = Math.Max(maximum, arr[i]); } // Check the required condition if (sum % 2 == 0 && maximum <= sum / 2) return true; return false; } // Driver code public static void Main () { int []arr = { 1, 2, 1, 2, 2 }; int n = arr.Length; if (checkZeroArray(arr, n) == true) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by AnkitRai01
Javascript
<script> // JavaScript implementation of the above approach // Function that returns true if all // the array elements can be made // 0 with the given operation function checkZeroArray(arr,n) { // Find the maximum element // and the sum let sum = 0, maximum = Number.MIN_VALUE; for (let i = 0; i < n; i++) { sum = sum + arr[i]; maximum = Math.max(maximum, arr[i]); } // Check the required condition if (sum % 2 == 0 && maximum <= sum / 2) return true; return false; } // Driver code let arr=[1, 2, 1, 2, 2 ]; let n = arr.length; if (checkZeroArray(arr, n) == true) document.write("Yes"); else document.write("No"); // This code is contributed by unknown2108 </script>
Yes
Complejidad de tiempo: O(n)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por AmanGupta65 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA