Smallest number dividing minimum number of elements in the array | Set 2 | Function to return the smallest number that divides minimum number of elements in the given array ; m stores the maximum in the array ; Frequency array ; Sieve ; Incrementing j ; If no multiples of j are in the array ; Driver code
function findMin ( arr , n ) { var m = 0 ; for ( var i = 0 ; i < n ; i ++ ) m = Math . max ( m , arr [ i ] ) ; var freq = Array ( m + 2 ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) freq [ arr [ i ] ] ++ ; for ( var i = 1 ; i <= m + 1 ; i ++ ) { var j = i ; var cnt = 0 ; while ( j <= m ) { cnt += freq [ j ] ; j += i ; } if ( ! cnt ) return i ; } return m + 1 ; } var arr = [ 2 , 12 , 6 ] ; var n = arr . length ; document . write ( findMin ( arr , n ) ) ;