Replace all elements by difference of sums of positive and negative numbers after that element | Function to prvar the array elements ; Function to replace all elements with absolute difference of absolute sums of positive and negative elements ; Calculate absolute sums of positive and negative elements in range i + 1 to N ; Calculate difference of both sums ; Replace i - th elements with absolute difference ; Driver code
function printArray ( N , arr ) { for ( i = 0 ; i < N ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ; } function replacedArray ( N , arr ) { var pos_sum , neg_sum , i , j , diff ; for ( i = 0 ; i < N ; i ++ ) { pos_sum = 0 ; neg_sum = 0 ; for ( j = i + 1 ; j < N ; j ++ ) { if ( arr [ j ] > 0 ) pos_sum += arr [ j ] ; else neg_sum += arr [ j ] ; } diff = Math . abs ( pos_sum ) - Math . abs ( neg_sum ) ; arr [ i ] = Math . abs ( diff ) ; } } var N = 5 ; var arr = [ 1 , - 1 , 2 , 3 , - 2 ] ; replacedArray ( N , arr ) ; printArray ( N , arr ) ; N = 6 ; var arr1 = [ - 3 , - 4 , - 2 , 5 , 1 , - 2 ] ; replacedArray ( N , arr1 ) ; printArray ( N , arr1 ) ;