Dada una array de enteros arr[] , la tarea es verificar si esa array contiene todos los divisores de algún entero.
Ejemplos:
Entrada: arr[] = { 2, 3, 1, 6}
Salida: Sí
La array contiene todos los divisores de 6
Entrada: arr[] = { 12, 2, 5, 3, 6, 4, 1}
Salida: No
Enfoque: si la array contiene todos los divisores de un entero en particular, digamos X , entonces el elemento máximo en la array arr[] es el entero X. Ahora, encuentre el elemento máximo de la array arr[] y calcule todos sus divisores y guárdelo en un vector b . Si el arreglo arr[] y el vector b son iguales, entonces el arreglo contiene todos los divisores de un entero en particular; de lo contrario, no.
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 arr[] // contains all the divisors of some integer bool checkDivisors(int a[], int n) { // Maximum element from the array int X = *max_element(a, a + n); // Vector to store divisors // of the maximum element i.e. X vector<int> b; // Store all the divisors of X for (int i = 1; i * i <= X; i++) { if (X % i == 0) { b.push_back(i); if (X / i != i) b.push_back(X / i); } } // If the lengths of a[] // and b are different // return false if (b.size() != n) return false; // Sort a[] and b sort(a, a + n); sort(b.begin(), b.end()); for (int i = 0; i < n; i++) { // If divisors are not // equal return false if (b[i] != a[i]) return false; } return true; } // Driver code int main() { int arr[] = { 8, 1, 2, 12, 48, 6, 4, 24, 16, 3 }; int N = sizeof(arr) / sizeof(arr[0]); if (checkDivisors(arr, N)) cout << "Yes"; else cout << "No"; return 0; }
Java
// Java implementation of the approach import java.util.*; class GFG { // returns th maximum element of the array static int max_element(int a[] ) { int m = a[0]; for(int i = 0; i < a.length; i++) m = Math.max(a[i], m); return m; } // Function that returns true if arr[] // contains all the divisors of some integer static boolean checkDivisors(int a[], int n) { // Maximum element from the array int X = max_element(a); // Vector to store divisors // of the maximum element i.e. X Vector<Integer> b=new Vector<Integer>(); // Store all the divisors of X for (int i = 1; i * i <= X; i++) { if (X % i == 0) { b.add(i); if (X / i != i) b.add(X / i); } } // If the lengths of a[] // and b are different // return false if (b.size() != n) return false; // Sort a[] and b Arrays.sort(a); Collections.sort(b); for (int i = 0; i < n; i++) { // If divisors are not // equal return false if (b.get(i) != a[i]) return false; } return true; } // Driver code public static void main(String args[]) { int arr[] = { 8, 1, 2, 12, 48, 6, 4, 24, 16, 3 }; int N = arr.length; if (checkDivisors(arr, N)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by Arnab Kundu
Python3
# Python 3 implementation of the approach from math import sqrt # Function that returns true if arr[] # contains all the divisors of some integer def checkDivisors(a,n): # Maximum element from the array X = max(a) # Vector to store divisors # of the maximum element i.e. X b = [] # Store all the divisors of X for i in range(1,int(sqrt(X))+1): if (X % i == 0): b.append(i) if (X // i != i): b.append(X // i) # If the lengths of a[] # and b are different # return false if (len(b) != n): return False # Sort a[] and b a.sort(reverse = False) b.sort(reverse = False) for i in range(n): # If divisors are not # equal return false if (b[i] != a[i]): return False return True # Driver code if __name__ == '__main__': arr = [8, 1, 2, 12, 48,6, 4, 24, 16, 3] N = len(arr) if (checkDivisors(arr, N)): print("Yes") else: print("No") # This code is contributed by # Surendra_Gangwar
C#
// C# implementation of the approach using System; using System.Collections.Generic; class GFG { // returns th maximum element of the array static int max_element(int []a ) { int m = a[0]; for(int i = 0; i < a.Length; i++) m = Math.Max(a[i], m); return m; } // Function that returns true if arr[] // contains all the divisors of some integer static bool checkDivisors(int []a, int n) { // Maximum element from the array int X = max_element(a); // Vector to store divisors // of the maximum element i.e. X List<int> b = new List<int>(); // Store all the divisors of X for (int i = 1; i * i <= X; i++) { if (X % i == 0) { b.Add(i); if (X / i != i) b.Add(X / i); } } // If the lengths of a[] // and b are different // return false if (b.Count != n) return false; // Sort a[] and b Array.Sort(a); b.Sort(); for (int i = 0; i < n; i++) { // If divisors are not // equal return false if (b[i] != a[i]) return false; } return true; } // Driver code public static void Main(String []args) { int []arr = { 8, 1, 2, 12, 48, 6, 4, 24, 16, 3 }; int N = arr.Length; if (checkDivisors(arr, N)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by Princi Singh
Javascript
<script> // Javascript implementation of the approach // Function that returns true if arr[] // contains all the divisors of some integer function checkDivisors(a, n) { // Maximum element from the array let X = Math.max(...a); // Vector to store divisors // of the maximum element i.e. X let b = []; // Store all the divisors of X for (let i = 1; i * i <= X; i++) { if (X % i == 0) { b.push(i); if (parseInt(X / i) != i) b.push(parseInt(X / i)); } } // If the lengths of a[] // and b are different // return false if (b.length != n) return false; // Sort a[] and b a.sort((x,y) => x - y); b.sort((x,y) => x - y); for (let i = 0; i < n; i++) { // If divisors are not // equal return false if (b[i] != a[i]) return false; } return true; } // Driver code let arr = [ 8, 1, 2, 12, 48, 6, 4, 24, 16, 3 ]; let N = arr.length; if (checkDivisors(arr, N)) document.write("Yes"); else document.write("No"); </script>
Yes
Complejidad de tiempo: O((n * log n) + max(arr)), donde max(arr) es el elemento más grande de la array arr.
Espacio auxiliar: O(max(arr))
Publicación traducida automáticamente
Artículo escrito por rupesh_rao y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA