Maximize array product by changing any array element arr [ i ] to ( | Function to find array with maximum product by changing array elements to ( - 1 ) arr [ i ] - 1 ; Traverse the array ; Applying the operation on all the positive elements ; Stores maximum element in array ; Stores index of maximum element ; Check if current element is greater than the maximum element ; Find index of the maximum element ; Perform the operation on the maximum element ; Print the elements of the array ; Driver Code ; Function Call
function findArrayWithMaxProduct ( arr , N ) { for ( var i = 0 ; i < N ; i ++ ) if ( arr [ i ] >= 0 ) { arr [ i ] = - arr [ i ] - 1 ; } if ( N % 2 === 1 ) { var max_element = - 1 ; var index = - 1 ; for ( var i = 0 ; i < N ; i ++ ) if ( Math . abs ( arr [ i ] ) > max_element ) { max_element = Math . abs ( arr [ i ] ) ; index = i ; } arr [ index ] = - arr [ index ] - 1 ; } for ( var i = 0 ; i < N ; i ++ ) document . write ( arr [ i ] + " " ) ; } var arr = [ - 3 , 0 , 1 ] ; var N = arr . length ; findArrayWithMaxProduct ( arr , N ) ;