Dada una array de n elementos, puede duplicar o triplicar los elementos de la array cualquier número de veces. Después de todas las operaciones, compruebe si es posible hacer que todos los elementos de la array sean iguales.
Ejemplos:
Input : A[] = {75, 150, 75, 50} Output : Yes Explanation : Here, 75 should be doubled twice and 150 should be doubled once and 50 should be doubled once and tripled once.Then, all the elements will be equal to 300. Input : A[] = {100, 151, 200} Output : No Explanation : No matter what we do all elements in the array could not be equal.
La idea es dividir repetidamente cada elemento por 2 y 3 hasta que el elemento sea divisible. Después de este paso, si todos los elementos se vuelven iguales, entonces la respuesta es sí.
¿Como funciona esto? Sabemos que todo número entero se puede expresar como producto de números primos 2 a .3 b .5 c .7 d ….. . Entonces, en nuestro problema podemos aumentar a y b duplicando (*2) o triplicando (*3). Podemos hacer que a y b de todos los elementos de la array sean iguales multiplicando por 2 o por 3. Pero los números también tienen otros números primos en su representación del producto, no podemos cambiar sus potencias. Entonces, para que todos los números sean iguales, deben tener potencias en otros números primos iguales desde el principio. Podemos comprobarlo dividiendo todos los números entre dos o tres tantas veces como sea posible. Entonces todos deberían ser iguales.
Implementación:
C++
// C++ program to check if all numbers can // be made equal by repeated division of 2 // and 3 #include <bits/stdc++.h> using namespace std; bool canMakeEqual(int a[], int n) { for (int i = 0; i < n; i++) { // continuously divide every number by 2 while (a[i] % 2 == 0) a[i] = a[i] / 2; // continuously divide every number by 3 while (a[i] % 3 == 0) a[i] = a[i] / 3; } // Check if all numbers same for (int i = 1; i < n; i++) if (a[i] != a[0]) return false; return true; } // Driver Code int main() { int A[] = { 75, 150, 75, 50 }; int n = sizeof(A) / sizeof(A[0]); if (canMakeEqual(A, n)) cout << "Yes"; else cout << "No"; return 0; }
Java
// Java program to check if all numbers can // be made equal by repeated division of 2 // and 3 import java.util.*; class GFG { static Boolean canMakeEqual(int a[], int n) { for (int i = 0; i < n; i++) { // Continuously divide every number by 2 while (a[i] % 2 == 0) a[i] = a[i] / 2; // Continuously divide every number by 3 while (a[i] % 3 == 0) a[i] = a[i] / 3; } // Check if all numbers same for (int i = 1; i < n; i++) if (a[i] != a[0]) return false; return true; } // Driver Code public static void main(String[] args) { int A[] = { 75, 150, 75, 50 }; int n = A.length; if (canMakeEqual(A, n)) System.out.print("Yes"); else System.out.print("No"); } } // This code is contributed by 'Gitanjali'.
Python3
# Python3 code to check if all numbers can # be made equal by repeated division of 2 # and 3 def canMakeEqual( a , n ): for i in range(n): # continuously divide every number by 2 while a[i] % 2 == 0: a[i] = int(a[i] / 2) # continuously divide every number by 3 while a[i] % 3 == 0: a[i] = int(a[i] / 3) # Check if all numbers same for i in range(1,n): if a[i] != a[0]: return False return True # Driver Code A = [ 75, 150, 75, 50 ] n = len(A) print("Yes" if canMakeEqual(A, n) else "No") # This code is contributed by "Sharad_Bhardwaj".
C#
// C# program to check if all numbers can // be made equal by repeated division of 2 // and 3 using System; class GFG { static Boolean canMakeEqual(int []a, int n) { for (int i = 0; i < n; i++) { // Continuously divide every number by 2 while (a[i] % 2 == 0) a[i] = a[i] / 2; // Continuously divide every number by 3 while (a[i] % 3 == 0) a[i] = a[i] / 3; } // Check if all numbers same for (int i = 1; i < n; i++) if (a[i] != a[0]) return false; return true; } // Driver Code public static void Main() { int []A = { 75, 150, 75, 50 }; int n = A.Length; if (canMakeEqual(A, n)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by 'vt_m'.
PHP
<?php // PHP program to check if // all numbers can be made // equal by repeated division // of 2 and 3 function canMakeEqual($a, $n) { for ($i = 0; $i < $n; $i++) { // continuously divide // every number by 2 while ($a[$i] % 2 == 0) $a[$i] = $a[$i] / 2; // continuously divide // every number by 3 while ($a[$i] % 3 == 0) $a[$i] = $a[$i] / 3; } // Check if all numbers same for ($i = 1; $i < $n; $i++) if ($a[$i] != $a[0]) return false; return true; } // Driver Code $A = array(75, 150, 75, 50); $n = sizeof($A); if (canMakeEqual($A, $n)) echo "Yes"; else echo "No"; // This code is contributed by aj_36 ?>
Javascript
<script> // Javascript program to check if all numbers can // be made equal by repeated division of 2 // and 3 function canMakeEqual(a, n) { for(let i = 0; i < n; i++) { // Continuously divide every number by 2 while (a[i] % 2 == 0) a[i] = a[i] / 2; // Continuously divide every number by 3 while (a[i] % 3 == 0) a[i] = a[i] / 3; } // Check if all numbers same for(let i = 1; i < n; i++) if (a[i] != a[0]) return false; return true; } // Driver code let A = [ 75, 150, 75, 50 ]; let n = A.length; if (canMakeEqual(A, n)) document.write("Yes"); else document.write("No"); // This code is contributed by suresh07 </script>
Yes
Publicación traducida automáticamente
Artículo escrito por Harsha_Mogali y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA