Check if the given array contains all the divisors of some integer | 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
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 ( " " ) ; else document . write ( " " ) ;