Dada una array arr[] de tamaño N y un número entero K. La tarea es encontrar el número de dígitos K más grande divisible por todos los números de arr[].
Ejemplos:
Entrada: arr[] = {2, 3, 5}, K = 3
Salida: 990
Explicación: 990 es el mayor número de 3 dígitos divisible por 2, 3 y 5.Entrada: arr[] = {91, 93, 95}, K = 3
Salida: -1
Explicación: No hay un número de 3 dígitos divisible por todos los 91, 93 y 95.
Enfoque: La solución se basa en la idea similar a encontrar el número de dígitos K más grande divisible por X. Siga los pasos que se mencionan a continuación.
- Encuentre MCM de todos los números de la array arr[]
- Encuentra el múltiplo más grande de MCM que tiene K dígitos usando la fórmula:
MCM(arr) * ((10 K -1)/MCM(arr))
A continuación se muestra la implementación del enfoque anterior.
C++
// C++ code to implement above approach #include <bits/stdc++.h> using namespace std; int __gcd(int a, int b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // Base case if (a == b) return a; // a is greater if (a > b) return __gcd(a - b, b); return __gcd(a, b - a); } // Function to find LCM of the array int findLCM(int arr[], int n, int idx) { // lcm(a, b) = (a*b / gcd(a, b)) if (idx == n - 1) { return arr[idx]; } int a = arr[idx]; int b = findLCM(arr, n, idx + 1); double gcd = __gcd(a, b); return (a * (int)floor(b / gcd)); } // Function to find the number int findNum(int arr[], int n, int K) { int lcm = findLCM(arr, n, 0); int ans = (int)floor((pow(10, K) - 1) / lcm); ans = (ans) * lcm; if (ans == 0) return -1; return ans; } // Driver code int main() { int arr[] = { 2, 3, 5 }; int n = sizeof(arr) / sizeof(arr[0]); int K = 3; cout << findNum(arr, n, K); } // This code is contributed by Samim Hossain Mondal.
Java
// Java code to implement above approach class GFG { static int __gcd(int a, int b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // Base case if (a == b) return a; // a is greater if (a > b) return __gcd(a - b, b); return __gcd(a, b - a); } // Function to find LCM of the array static int findLCM(int []arr, int idx) { // lcm(a, b) = (a*b / gcd(a, b)) if (idx == arr.length - 1) { return arr[idx]; } int a = arr[idx]; int b = findLCM(arr, idx + 1); double gcd = __gcd(a, b); return (a * (int)Math.floor(b / gcd)); } // Function to find the number static int findNum(int []arr, int K) { int lcm = findLCM(arr, 0); int ans = (int)Math.floor((Math.pow(10, K) - 1) / lcm); ans = (ans) * lcm; if (ans == 0) return -1; return ans; } // Driver code public static void main(String []args) { int []arr = { 2, 3, 5 }; int K = 3; System.out.println(findNum(arr, K)); } } // This code is contributed by 29AjayKumar
Python3
# python code to implement above approach import math # Function to find LCM of the array def findLCM(arr, idx): # lcm(a, b) = (a*b / gcd(a, b)) if (idx == len(arr) - 1): return arr[idx] a = arr[idx] b = findLCM(arr, idx + 1) return (a * b // math.gcd(a, b)) # Function to find the number def findNum(arr, K): lcm = findLCM(arr, 0) ans = (pow(10, K) - 1) // lcm ans = (ans)*lcm if (ans == 0): return -1 return ans # Driver code if __name__ == "__main__": arr = [2, 3, 5] K = 3 print(findNum(arr, K)) # This code is contributed by rakeshsahni
C#
// C# code to implement above approach using System; using System.Collections; class GFG { static int __gcd(int a, int b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // Base case if (a == b) return a; // a is greater if (a > b) return __gcd(a - b, b); return __gcd(a, b - a); } // Function to find LCM of the array static int findLCM(int []arr, int idx) { // lcm(a, b) = (a*b / gcd(a, b)) if (idx == arr.Length - 1) { return arr[idx]; } int a = arr[idx]; int b = findLCM(arr, idx + 1); double gcd = __gcd(a, b); return (a * (int)Math.Floor(b / gcd)); } // Function to find the number static int findNum(int []arr, int K) { int lcm = findLCM(arr, 0); int ans = (int)Math.Floor((Math.Pow(10, K) - 1) / lcm); ans = (ans) * lcm; if (ans == 0) return -1; return ans; } // Driver code public static void Main() { int []arr = { 2, 3, 5 }; int K = 3; Console.Write(findNum(arr, K)); } } // This code is contributed by Samim Hossain Mondal.
Javascript
<script> // JavaScript code to implement above approach function __gcd(a, b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // Base case if (a == b) return a; // a is greater if (a > b) return __gcd(a - b, b); return __gcd(a, b - a); } // Function to find LCM of the array function findLCM(arr, idx) { // lcm(a, b) = (a*b / gcd(a, b)) if (idx == arr.length - 1) { return arr[idx]; } let a = arr[idx]; let b = findLCM(arr, idx + 1); return Math.floor((a * b / __gcd(a, b))); } // Function to find the number function findNum(arr, K) { let lcm = findLCM(arr, 0); let ans = Math.floor((Math.pow(10, K) - 1) / lcm); ans = (ans) * lcm; if (ans == 0) return -1; return ans; } // Driver code let arr = [ 2, 3, 5 ]; let K = 3; document.write(findNum(arr, K)); // This code is contributed by Potta Lokesh </script>
990
Complejidad de Tiempo: O(N*logD) donde D es el elemento máximo del arreglo
Espacio Auxiliar: O(1)