[Java] Runtime: 17ms, faster than 96.63% || Prefix sum || Time O(n) || Space O(1)
\nclass Solution {\n    public int minimumAverageDifference(int[] nums) {\n        long min = Integer.MAX_VALUE, sum = 0;\n        for (int i : nums) sum += i;\
```\nclass Solution {\n    public int minimumAverageDifference(int[] nums) {\n        long min = Integer.MAX_VALUE, sum = 0;\n        for (int i : nums) sum += i;\n        int leftIndex = 0, resultIndex = -1;\n        long prefixSum = 0;\n\n        while (leftIndex < nums.length) {\n            prefixSum += nums[leftIndex];\n            long leftAverage = prefixSum / (leftIndex + 1);\n            long sumRight = sum - prefixSum;\n            if (sumRight != 0) sumRight /= nums.length - leftIndex - 1;\n            long res = Math.abs(leftAverage - sumRight);\n            if (res < min) {\n                min = res;\n                resultIndex = leftIndex;\n            }\n            leftIndex++;\n        }\n        return resultIndex;\n    }\n}\n```\n\n![image](https://assets.leetcode.com/users/images/5d0ba1a4-fea0-4734-bc00-cdf00e45d5b4_1670122282.0697427.png)\n
['Java']