Check if the given array contains all the divisors of some integer
Last Updated :
30 May, 2022
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++
#include <bits/stdc++.h>
using namespace std;
bool checkDivisors( int a[], int n)
{
int X = *max_element(a, a + n);
vector< int > b;
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 (b.size() != n)
return false ;
sort(a, a + n);
sort(b.begin(), b.end());
for ( int i = 0; i < n; i++) {
if (b[i] != a[i])
return false ;
}
return true ;
}
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
import java.util.*;
class GFG
{
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;
}
static boolean checkDivisors( int a[], int n)
{
int X = max_element(a);
Vector<Integer> b= new Vector<Integer>();
for ( int i = 1 ; i * i <= X; i++)
{
if (X % i == 0 )
{
b.add(i);
if (X / i != i)
b.add(X / i);
}
}
if (b.size() != n)
return false ;
Arrays.sort(a);
Collections.sort(b);
for ( int i = 0 ; i < n; i++)
{
if (b.get(i) != a[i])
return false ;
}
return true ;
}
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" );
}
}
|
Python3
from math import sqrt
def checkDivisors(a,n):
X = max (a)
b = []
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 ( len (b) ! = n):
return False
a.sort(reverse = False )
b.sort(reverse = False )
for i in range (n):
if (b[i] ! = a[i]):
return False
return True
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" )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
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;
}
static bool checkDivisors( int []a, int n)
{
int X = max_element(a);
List< int > b = new List< int >();
for ( int i = 1; i * i <= X; i++)
{
if (X % i == 0)
{
b.Add(i);
if (X / i != i)
b.Add(X / i);
}
}
if (b.Count != n)
return false ;
Array.Sort(a);
b.Sort();
for ( int i = 0; i < n; i++)
{
if (b[i] != a[i])
return false ;
}
return true ;
}
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" );
}
}
|
Javascript
<script>
function checkDivisors(a, n)
{
let X = Math.max(...a);
let b = [];
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 (b.length != n)
return false ;
a.sort((x,y) => x - y);
b.sort((x,y) => x - y);
for (let i = 0; i < n; i++) {
if (b[i] != a[i])
return false ;
}
return true ;
}
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>
|
Time Complexity: O((n * log n) + max(arr)), where max(arr) is the largest element of the array arr.
Auxiliary Space: O(max(arr))
Share your thoughts in the comments
Please Login to comment...