Smallest number dividing minimum number of elements in the Array | Function to return the smallest number that divides minimum number of elements ; m stores the maximum in the array ; Frequency table ; Loop to factorize ; sqrt factorization of the numbers ; Finding the smallest number with zero multiples ; Driver code
function findMin ( arr , n ) { var m = 0 ; for ( var i = 0 ; i < n ; i ++ ) m = Math . max ( m , arr [ i ] ) ; var cnt = Array ( m + 2 ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 1 ; j * j <= arr [ i ] ; j ++ ) { if ( arr [ i ] % j == 0 ) { if ( j * j == arr [ i ] ) cnt [ j ] ++ ; else cnt [ j ] ++ , cnt [ arr [ i ] / j ] ++ ; } } } for ( var i = 1 ; i <= m + 1 ; i ++ ) if ( cnt [ i ] == 0 ) { return i ; } return - 1 ; } var arr = [ 2 , 12 , 6 ] ; var n = arr . length ; document . write ( findMin ( arr , n ) ) ;