[JAVA] |  BRUTEFORCE => BETTER => OPTIMAL | (Detailed / Intuitive Explanation )
BRUTEFORCE => BETTER => OPTIMAL (Detailed Explanation )\n\n    //Bruteforce Approach\n    public static int[] getSumAbsoluteDifferences(int[]arr)\n\t{\n        
BRUTEFORCE => BETTER => OPTIMAL (Detailed Explanation )\n```\n    //Bruteforce Approach\n    public static int[] getSumAbsoluteDifferences(int[]arr)\n\t{\n         int n=arr.length;\n         int[] res=new int[n];\n         for(int i=0;i<n;i++)\n            for(int j=0;j<n;j++)\n               if(i!=j)\n                  res[i]+=Math.abs(arr[i]-arr[j]); \n        return res;    \n\t}\n```\n\n```\n/*\n//Efficient Approach\n\nlet length=arr.length\nfor every element(i.e arr[i]),the absolute difference is :\n\n=> |arr[i]-arr[0]|+|arr[i]-arr[1]|+.......+|arr[i]-arr[i-1]|+|arr[i]-arr[i]|+|arr[i]-arr[i+1]|+...........+|arr[i]-arr[length-2]|+|arr[i]-arr[length-1]|\n\n=> (arr[i]-arr[0])+(arr[i]-arr[1])+.......+(arr[i]-arr[i-1])+(arr[i]-arr[i])+(arr[i+1]-arr[i])+...........+(arr[length-2]-arr[i])+(arr[length-1]-arr[i]) \n\n=> (arr[i]-arr[0])+(arr[i]-arr[1])+....... +(arr[i]-arr[i-1])+ 0 +(arr[i+1]-arr[i])+...........+(arr[length-2]-arr[i])+(arr[length-1]-arr[i]) \n\n=> (arr[i]*i - (arr[0]+arr[1]+...arr[i-1])) + ((arr[i+1]+arr[i+2]+....arr[length-1]) - arr[i]*(length-1-i))\n\n=> ((arr[i]*i) - prefixsum[i]) + (postfixsum[i] - (arr[i]*(length-1-i)))\n\nNote :\n\nin Expression : "arr[i]*i" \'i\' is the count of arr[i] to the left of arr[i].\n\nSimilarly,\n\nin Expression : "arr[i]*(length-1-i)" \'(length-1-i)\' is the count of arr[i] to the right of arr[i].\n\nOPTIMISATION :\nwe know ;\nprefixSum[i]= (arr[0]+arr[1]+...arr[i-1]);\nand\npostfixsum[i]=(arr[i+1]+arr[i+2]+....arr[length-1])\n\n(arr[i+1]+arr[i+2]+....arr[length-1] = (arr[0]+arr[1]+...arr[length-1]) - (arr[0]+arr[1]+...arr[i-1]) -arr[i] = arraySum-prefixSum[i]-arr[i];\n\nthus, postfixSum[i]= arraySum - prefixSum[i] - arr[i];\n*/\n```\n```\n       //Intuitive Implementation\n\n      public  int[] getSumAbsoluteDifferences(int[]arr) \n      {\n          int n=arr.length;\n          int prefixSum[] = new int[n];\n          int postfixSum[] = new int[n];\n     \n          for(int i=1;i<n;i++) //prefixSum[0]=0\n             prefixSum[i]=prefixSum[i-1]+arr[i-1];\n    \n          for(int i=n-2;i>=0;i--) //postfixSum[n-1]=0\n            postfixSum[i]=postfixSum[i+1]+arr[i+1];\n        \n          int[] res=new int[n];\n          for(int i=0;i<n;i++) \n            res[i]=(i*arr[i] - prefixSum[i]) + (postfixSum[i] - (n-1-i)*arr[i]);\n    \n          return res;    \n\t  }\n```\n```\n       //Optimal Implementation\n\n        public  int[] getSumAbsoluteDifferences(int[]arr,int n)\n\t    {\n          int arrSum=arr[0];\n          int prefixSum[] = new int[n];\n     \n          for(int i=1;i<n;i++) //prefixSum[0]=0\n          {\n            prefixSum[i]=prefixSum[i-1]+arr[i-1];\n            arrSum+=arr[i];\n          }\n    \n          int[] res=new int[n];\n          for(int i=0;i<n;i++)\n          {\n            int postfixSumAt_i=arrSum-prefixSum[i]-arr[i];\n            res[i]=(i*arr[i] - prefixSum[i]) + (postfixSumAt_i - (n-1-i)*arr[i]);  \n          }\n        \n          return res;   \n       }\n```\n
['Java']