Array formed using sum of absolute differences of that element with all other elements | Function to return the new array ; Length of the arraylist ; Initialize the Arraylist ; Sum of absolute differences of element with all elements ; Initialize let sum to 0 ; Add the value of sum to ans ; Return the final ans ; Given array arr [ ] ; Function Call
function calculate ( arr ) { let n = arr . length ; let ans = [ ] ; for ( let i = 0 ; i < arr . length ; i ++ ) { let sum = 0 ; for ( let j = 0 ; j < arr . length ; j ++ ) { sum += Math . abs ( arr [ i ] - arr [ j ] ) ; } ans . push ( sum ) ; } return ans ; } let arr = [ 2 , 3 , 5 , 6 ] ; document . write ( calculate ( arr ) ) ;