JAVA | 3 solutions | Brute -> Optimal ✅
Please Upvote :D\n##### 1. Brute force approach:\nCalculating the right and left average using nested loops for every index of nums.\njava []\nclass Solution {\
### **Please Upvote** :D\n##### 1. Brute force approach:\nCalculating the right and left average using nested loops for every index of nums.\n``` java []\nclass Solution {\n    public int minimumAverageDifference(int[] nums) {\n        int n = nums.length;\n        int minAvgDiff = Integer.MAX_VALUE, ans = -1;\n\n        for (int i = 0; i < n; i++) {\n            int leftAvg = 0;\n\n            for (int left = 0; left <= i; left++) {\n                leftAvg += nums[left];\n            }\n\n            leftAvg /= (i + 1);\n\n            int rightAvg = 0;\n\n            for (int right = i + 1; right < n; right++) {\n                rightAvg += nums[right];\n            }\n\n            if (i != n - 1) rightAvg /= (n - i - 1);\n\n            int currDiff = Math.abs(leftAvg - rightAvg);\n\n            if (currDiff < minAvgDiff) {\n                minAvgDiff = currDiff;\n                ans = i;\n            }\n        }\n\n        return ans;\n    }\n}\n\n// TC: O(n ^ 2), SC: O(1)\n```\n##### 2. Using prefix-sum and suffix-sum arrays:\nWe precompute the prefix sums and suffix sums of each index of nums and sore them in separate arrays in order to save us from running nested loops.\n``` java []\nclass Solution {\n    public int minimumAverageDifference(int[] nums) {\n        int n = nums.length;\n        long[] prefix = new long[n + 1];\n        long[] suffix = new long[n + 1];\n\n        for (int i = 0; i < n; i++) {\n            prefix[i + 1] = prefix[i] + nums[i];\n        }\n\n        for (int i = n - 1; i >= 0; i--) {\n            suffix[i] = suffix[i + 1] + nums[i];\n        }\n\n        int minAvgDiff = Integer.MAX_VALUE;\n        int ans = -1;\n\n        for (int i = 0; i < n; i++) {\n            long leftAvg = prefix[i + 1] / (i + 1);\n            long rightAvg = (i != n - 1)? suffix[i + 1] / (n - i - 1) : 0;\n\n            int currDiff = (int) Math.abs(leftAvg - rightAvg);\n\n            if (currDiff < minAvgDiff) {\n                minAvgDiff = currDiff;\n                ans = i;\n            }\n        }\n\n        return ans;\n    }\n}\n\n// TC: 3 * O(n) => O(n)\n// SC: 2 * O(n) => O(n)\n```\n##### 3. Without using extra space:\nWe precompute the total sum of every element of nums then run a loop to calculate left sum and left average. The right sum of that index will be given by subtracting the current sum at that index from the total sum and we can calculate the right average accordingly.\n``` java []\nclass Solution {\n    public int minimumAverageDifference(int[] nums) {\n        int n = nums.length;\n        int ans = -1, minAvgDiff = Integer.MAX_VALUE;\n\n        long totalSum = 0;\n        for (int i : nums) totalSum += i;\n\n        long currSum = 0;\n\n        for (int i = 0; i < n; i++) {\n            currSum += nums[i];\n\n            long leftAvg = currSum / (i + 1);\n            long rightAvg = (i != n - 1)? (totalSum - currSum) / (n - i - 1) : 0;\n\n            int currDiff = (int) Math.abs(leftAvg - rightAvg);\n\n            if (currDiff < minAvgDiff) {\n                minAvgDiff = currDiff;\n                ans = i;\n            }\n        }\n\n        return ans;\n    }\n}\n\n// TC: 2 * O(n) => O(n)\n// SC: O(1)\n```
['Prefix Sum', 'Java']