Dada una array arr[] de n enteros y un entero k . La tarea es hacer que todos los elementos de arr[] sean iguales a la operación dada. En una sola operación, cualquier número no negativo x ≤ k (puede ser un valor de punto flotante) se puede agregar a cualquier elemento de la array y k se actualizará como k = k – x . Imprimir Sí es posible, de lo contrario, imprimir No.
Ejemplos:
Entrada: k = 8, arr[] = {1, 2, 3, 4}
Salida: Sí
1 + 3,5 = 4,5
2 + 2,5 = 4,5
3 + 1,5 = 4,5
4 + 0,5 = 4,5
3,5 + 2,5 + 1,5 + 0,5 = 8 = k
Entrada: k = 2, arr[] = {1, 2, 3, 4}
Salida: -1
Enfoque: dado que la tarea es hacer que todos los elementos de la array sean iguales y el total de sumas debe ser exactamente k . Solo hay un único valor en el que podemos hacer que todos estos elementos sean iguales, es decir , (sum(arr) + k) / n . Si hay un elemento en la array que ya es mayor que este valor, entonces la respuesta no existe; de lo contrario, imprima Sí .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that returns true if all the elements // of the array can be made equal // with the given operation bool isPossible(int n, int k, int arr[]) { // To store the sum of the array elements // and the maximum element from the array int sum = arr[0], maxVal = arr[0]; for (int i = 1; i < n; i++) { sum += arr[i]; maxVal = max(maxVal, arr[i]); } if ((float)maxVal > (float)(sum + k) / n) return false; return true; } // Driver code int main() { int k = 8; int arr[] = { 1, 2, 3, 4 }; int n = sizeof(arr) / sizeof(arr[0]); if (isPossible(n, k, arr)) cout << "Yes"; else cout << "No"; return 0; }
Java
//Java implementation of the approach import java.io.*; class GFG { // Function that returns true if all // the elements of the array can be // made equal with the given operation static boolean isPossible(int n, int k, int arr[]) { // To store the sum of the array elements // and the maximum element from the array int sum = arr[0]; int maxVal = arr[0]; for (int i = 1; i < n; i++) { sum += arr[i]; maxVal = Math.max(maxVal, arr[i]); } if ((float)maxVal > (float)(sum + k) / n) return false; return true; } // Driver code public static void main (String[] args) { int k = 8; int arr[] = { 1, 2, 3, 4 }; int n = arr.length; if (isPossible(n, k, arr)) System.out.println ("Yes"); else System.out.println( "No"); } } // This code is contributed by @Tushil.
Python3
# Python 3 implementation of the approach # Function that returns true if all # the elements of the array can be # made equal with the given operation def isPossible(n, k, arr): # To store the sum of the array elements # and the maximum element from the array sum = arr[0] maxVal = arr[0]; for i in range(1, n): sum += arr[i] maxVal = max(maxVal, arr[i]) if (int(maxVal)> int((sum + k) / n)): return False return True # Driver code if __name__ == '__main__': k = 8 arr = [1, 2, 3, 4] n = len(arr) if (isPossible(n, k, arr)): print("Yes") else: print("No") # This code is contributed by # Surendra_Gangwar
C#
// C# implementation of the approach using System; class GFG { // Function that returns true if all // the elements of the array can be // made equal with the given operation static bool isPossible(int n, int k, int []arr) { // To store the sum of the array elements // and the maximum element from the array int sum = arr[0]; int maxVal = arr[0]; for (int i = 1; i < n; i++) { sum += arr[i]; maxVal = Math.Max(maxVal, arr[i]); } if ((float)maxVal > (float)(sum + k) / n) return false; return true; } // Driver code public static void Main() { int k = 8; int []arr = { 1, 2, 3, 4 }; int n = arr.Length; if (isPossible(n, k, arr)) Console.WriteLine("Yes"); else Console.WriteLine( "No"); } } // This code is contributed by Ryuga
PHP
<?php // PHP implementation of the approach // Function that returns true if // all the elements of the array // can be made equal with the given operation function isPossible($n, $k, $arr) { // To store the sum of the array elements // and the maximum element from the array $sum = $arr[0]; $maxVal = $arr[0]; for ($i = 1; $i < $n; $i++) { $sum += $arr[$i]; $maxVal = max($maxVal, $arr[$i]); } if ((float)$maxVal > (float)($sum + $k) / $n) return false; return true; } // Driver code $k = 8; $arr = array( 1, 2, 3, 4 ); $n = sizeof($arr) / sizeof($arr[0]); if (isPossible($n, $k, $arr)) echo "Yes"; else echo "No"; # This code is contributed by akt_miit. ?>
Javascript
<script> // Javascript implementation of the above approach // Function that returns true if all // the elements of the array can be // made equal with the given operation function isPossible(n, k, arr) { // To store the sum of the array elements // and the maximum element from the array let sum = arr[0]; let maxVal = arr[0]; for (let i = 1; i < n; i++) { sum += arr[i]; maxVal = Math.max(maxVal, arr[i]); } if (maxVal > (sum + k) / n) return false; return true; } // driver program let k = 8; let arr = [ 1, 2, 3, 4 ]; let n = arr.length; if (isPossible(n, k, arr)) document.write ("Yes"); else document.write( "No"); </script>
Yes
Complejidad de tiempo: O(n)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por Abdullah Aslam y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA