Find maximum value of the last element after reducing the array with given operations | Function to return the maximized value ; Overall minimum absolute value of some element from the array ; Add all absolute values ; Count positive and negative elements ; Both positive and negative values are present ; Only positive or negative values are present ; Driver code
function find_maximum_value ( a , n ) { var sum = 0 ; var minimum = Number . MAX_VALUE ; var pos = 0 , neg = 0 ; for ( i = 0 ; i < n ; i ++ ) { minimum = Math . min ( minimum , Math . abs ( a [ i ] ) ) ; sum += Math . abs ( a [ i ] ) ; if ( a [ i ] >= 0 ) pos += 1 ; else neg += 1 ; } if ( pos > 0 && neg > 0 ) return sum ; return ( sum - 2 * minimum ) ; } var a = [ 5 , 4 , 6 , 2 ] ; var n = a . length ; document . write ( find_maximum_value ( a , n ) ) ;