Java | O(N) solution | Beats 100%
Intuition\n\nThe Intuition is first sort the array and then find the minimum between 3 values ->\n\nWe can change 2 elements, and we have to find the absolute v
# Intuition\n\nThe Intuition is first sort the array and then find the minimum between 3 values ->\n\nWe can change 2 elements, and we have to find the absolute value, the minimum would always be 0. So we need to minimze the maximum value.\n\nTo do this, we have sort the array.\n\nNow, once sorted to minimze the maximum value, we have 3 options -\n\nChange the last 2 elements.\nChange the first 2 elements.\nChange the first and the last element\nAnd finally, we get the minimum value\n\n  Arrays.sort(nums);\n  int a=nums[nums.length-1]-nums[2];\n  int b=nums[nums.length-3]-nums[0];\n  int c=nums[nums.length-2]-nums[1];\n\n  return Math.min(a,Math.min(b,c));\n\nThis code works too, but we can skip the sorting process as well.\nOur end goal is to find 6 elements as we can see above and the indexes.\n\nThe six elements are ->\n\nmin, secondMin, thirdMin\nmax , secondMax , thirdMax;\n\nKeep track of indexes corresponding to the above values\nint a=-1, b=-1, c=-1, d=-1,e=-1,f=-1;\n\nOnce we have these, we can use the same sorting approach as above.\n\n# Complexity\n\nTime complexity:\nO(n)\n\nSpace complexity:\nO(1)\n\nCode\n\n\n# Code\n```\nclass Solution {\n    public int minimizeSum(int[] nums) {\n        int min = Integer.MAX_VALUE, secondMin = Integer.MAX_VALUE, thirdMin = Integer.MAX_VALUE;\n        int max = Integer.MIN_VALUE, secondMax = Integer.MIN_VALUE, thirdMax = Integer.MIN_VALUE;\n        int a=-1, b=-1, c=-1, d=-1,e=-1,f=-1;\n\n        for(int i=0; i<nums.length; i++) {\n            if(max <= nums[i]) {\n                a = i;\n                max = nums[i];\n            }\n            if(min >= nums[i]) {\n                f = i;\n                min = nums[i];\n            }\n        }\n\n        for(int i=0; i<nums.length; i++) {\n            if(secondMax <= nums[i] && i != a) {\n                b = i;\n                secondMax = nums[i];\n            }\n            if(secondMin >= nums[i] && i != f) {\n                e = i;\n                secondMin = nums[i];\n            }\n        }\n\n        for(int i=0; i<nums.length; i++) {\n            if(thirdMax <= nums[i] && i != a && i!= b) {\n                c = i;\n                thirdMax = nums[i];\n            }\n            if(thirdMin >= nums[i] && i != f && i != e) {\n                d = i;\n                thirdMin = nums[i];\n            }\n        }\n\n        int a1=Math.abs(nums[f]-nums[c]);\n\n        int b1=Math.abs(nums[d]-nums[a]);\n\n        int c1=Math.abs(nums[e]-nums[b]);\n        \n        //Return the Min value \n        return Math.min(a1,Math.min(b1,c1));\n    }\n}\n```
['Java']