Check if the given array contains all the divisors of some integer | C ++ implementation of the approach ; Function that returns true if arr [ ] contains all the divisors of some integer ; Maximum element from the array ; Vector to store divisors of the maximum element i . e . X ; Store all the divisors of X ; If the lengths of a [ ] and b are different return false ; Sort a [ ] and b ; If divisors are not equal return false ; Driver code
#include <bits/stdc++.h> NEW_LINE 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 ; }