Array formed using sum of absolute differences of that element with all other elements | C # program for the above approach ; Function to return the new array ; Length of the arraylist ; Initialize the Arraylist ; Sum of absolute differences of element with all elements ; Initialize int sum to 0 ; Add the value of sum to ans ; Return the final ans ; Driver Code ; Given array arr [ ] ; Function Call
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { private static List < int > calculate ( int [ ] arr ) { int n = arr . Length ; List < int > ans = new List < int > ( ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) { int sum = 0 ; for ( int j = 0 ; j < arr . Length ; j ++ ) { sum += Math . Abs ( arr [ i ] - arr [ j ] ) ; } ans . Add ( sum ) ; } return ans ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 3 , 5 , 6 } ; List < int > tmp = calculate ( arr ) ; Console . Write ( " [ " ) ; for ( int i = 0 ; i < tmp . Count ; i ++ ) { if ( i != tmp . Count - 1 ) { Console . Write ( tmp [ i ] + " , ▁ " ) ; } else { Console . Write ( tmp [ i ] ) ; } } Console . Write ( " ] " ) ; } }