Dados dos enteros n y m , en una sola operación n puede multiplicarse por 2 o por 3 . La tarea es convertir n en m con un número mínimo de operaciones dadas. Si es imposible convertir n en m con la operación dada, imprima -1 .
Ejemplos:
Entrada: n = 120, m = 51840
Salida: 7
120 * 2 * 2 * 2 * 2 * 3 * 3 * 3 = 51840
Entrada: n = 42, m = 42
Salida: 0
No requiere operación.
Entrada: n = 48, m = 72
Salida: -1
Enfoque: si m no es divisible por n , imprima -1 ya que n no se puede convertir en m con la operación dada. De lo contrario, podemos comprobar si, al dividir, el cociente tiene solo 2 y 3 como factores primos. En caso afirmativo, el resultado será la suma de las potencias de 2 y 3 ; de lo contrario, imprima -1
. 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 to return the minimum // operations required int minOperations(int n, int m) { if (m % n != 0) return -1; int minOperations = 0; int q = m / n; // Counting all 2s while (q % 2 == 0) { q = q / 2; minOperations++; } // Counting all 3s while (q % 3 == 0) { q = q / 3; minOperations++; } // If q contained only 2 and 3 // as the only prime factors // then it must be 1 now if (q == 1) return minOperations; return -1; } // Driver code int main() { int n = 120, m = 51840; cout << minOperations(n, m); return 0; }
Java
// Java implementation of the approach class GfG { // Function to return the minimum // operations required static int minOperations(int n, int m) { if (m % n != 0) return -1; int minOperations = 0; int q = m / n; // Counting all 2s while (q % 2 == 0) { q = q / 2; minOperations++; } // Counting all 3s while (q % 3 == 0) { q = q / 3; minOperations++; } // If q contained only 2 and 3 // as the only prime factors // then it must be 1 now if (q == 1) return minOperations; return -1; } // Driver code public static void main(String[] args) { int n = 120, m = 51840; System.out.println(minOperations(n, m)); } }
Python3
# Python 3 implementation of the approach # Function to return the minimum # operations required def minOperations(n, m): if (m % n != 0): return -1 minOperations = 0 q = int(m / n) # Counting all 2s while (q % 2 == 0): q = int(q / 2) minOperations += 1 # Counting all 3s while (q % 3 == 0): q = int(q / 3) minOperations += 1 # If q contained only 2 and 3 # as the only prime factors # then it must be 1 now if (q == 1): return minOperations return -1 # Driver code if __name__ == '__main__': n = 120 m = 51840 print(minOperations(n, m)) # This code is contributed by # Surendra_Gangwar
C#
// C# implementation of the approach using System; class GFG { // Function to return the minimum // operations required static int minOperations(int n, int m) { if (m % n != 0) return -1; int minOperations = 0; int q = m / n; // Counting all 2s while (q % 2 == 0) { q = q / 2; minOperations++; } // Counting all 3s while (q % 3 == 0) { q = q / 3; minOperations++; } // If q contained only 2 and 3 // as the only prime factors // then it must be 1 now if (q == 1) return minOperations; return -1; } // Driver code public static void Main() { int n = 120, m = 51840; Console.WriteLine(minOperations(n, m)); } } // This code is contributed // by Akanksha Rai
PHP
<?php // PHP implementation of the approach // Function to return the minimum // operations required function minOperations($n, $m) { if ($m % $n != 0) return -1; $minOperations = 0; $q = $m / $n; // Counting all 2s while ($q % 2 == 0) { $q = $q / 2; $minOperations++; } // Counting all 3s while ($q % 3 == 0) { $q = $q / 3; $minOperations++; } // If q contained only 2 and 3 // as the only prime factors // then it must be 1 now if ($q == 1) return $minOperations; return -1; } // Driver code $n = 120; $m = 51840; echo(minOperations($n, $m)); // This code is contributed by Code_Mech ?>
Javascript
<script> // javascript implementation of the approach // Function to return the minimum // operations required function minOperations(n , m) { if (m % n != 0) return -1; var minOperations = 0; var q = m / n; // Counting all 2s while (q % 2 == 0) { q = q / 2; minOperations++; } // Counting all 3s while (q % 3 == 0) { q = q / 3; minOperations++; } // If q contained only 2 and 3 // as the only prime factors // then it must be 1 now if (q == 1) return minOperations; return -1; } // Driver code var n = 120, m = 51840; document.write(minOperations(n, m)); // This code contributed by Rajput-Ji </script>
7
Complejidad del tiempo: O(log(m/n))
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por anshuman_7 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA