Minimize the sum of the array according the given condition | Function to return the minimum sum ; sort the array to find the minimum element ; finding the number to divide ; Checking to what instance the sum has decreased ; getting the max difference ; Driver Code
function findMin ( arr , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; arr . sort ( ) ; let min = arr [ 0 ] ; let max = 0 ; for ( let i = n - 1 ; i >= 1 ; i -- ) { let num = arr [ i ] ; let total = num + min ; let j ; for ( j = 2 ; j <= num ; j ++ ) { if ( num % j == 0 ) { let d = j ; let now = parseInt ( num / d ) + ( min * d ) ; let reduce = total - now ; if ( reduce > max ) max = reduce ; } } } document . write ( sum - max ) ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let n = arr . length ; findMin ( arr , n ) ;