# Check if the given array contains all the divisors of some integer

Given an integer array arr[], the task is to check if that array contains all the divisor of some integer.
Examples:

Input: arr[] = { 2, 3, 1, 6}
Output: Yes
The array contains all the divisors of 6
Input: arr[] = { 12, 2, 5, 3, 6, 4, 1}
Output: No

Approach: If the array contains all the divisors of a particular integer say X then the maximum element in the array arr[] is the integer X. Now, find the maximum element of the array arr[] and calculate all of its divisors and store it in a vector b. If array arr[] and vector b are equal then the array contains all the divisors of a particular integer, otherwise no.
Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``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 b=``new` `Vector();` `    ``// 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

 ``

Output:
`Yes`

Time Complexity: O((n * log n) + max(arr)), where max(arr) is the largest element of the array arr.

Auxiliary Space: O(max(arr))

Previous
Next