text: Minimum number of given operations required to reduce the array to 0 element | Function to return the minimum operations required ; Count the frequency of each element ; Maximum element from the array ; Find all the multiples of i ; Delete the multiples ; Increment the operations ; Driver code
code: function minOperations ( arr , n ) { let maxi , result = 0 ; let freq = new Array ( 1000001 ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { let x = arr [ i ] ; freq [ x ] ++ ; } maxi = Math . max ( ... arr ) ; for ( let i = 1 ; i <= maxi ; i ++ ) { if ( freq [ i ] != 0 ) { for ( let j = i * 2 ; j <= maxi ; j = j + i ) { freq [ j ] = 0 ; } result ++ ; } } return result ; } let arr = [ 2 , 4 , 2 , 4 , 4 , 4 ] ; let n = arr . length ; document . write ( minOperations ( arr , n ) ) ;