Beats 100% best intuitive solution - Must Read JAVA||PYTHON|| JAVASCRIPT||C++
Here is the Intuition\n\n\n1. Remove the smallest element, minElement, and the largest element maxElement, from nums.\n2. Add (minElement + maxElement) / 2 to a
#### Here is the Intuition\n\n\n1. Remove the **smallest element**, minElement, and the **largest element maxElement**, from nums.\n2. Add (minElement + maxElement) / 2 to averages.\n\n<br/>\n\n```\nwe know after sorting the smallest and largest will be the \nend points of the array\n\nso we just have to get the average of them and \nreturn the minimum average\n```\n\n\n##### Time Complexity:  O(NlogN)\n##### Space Complexity varies based on sorting algorithm used\n\nFor example in java for primitives Arrays.sort takes O(logN) space.\n\nHope this explanation Helps!\n\n[Follow up](https://leetcode.com/problems/number-of-distinct-averages/solutions/5359352/simple-java-solution-pattern-template-questions-attached/)\n\n# Code\n```java []\nclass Solution {\n    public double minimumAverage(int[] nums) {\n        \n        int left =0;\n        int right =nums.length-1;\n        double min = Double.MAX_VALUE;\n        Arrays.sort(nums);\n        \n        while(left<=right){\n            \n            double avg = nums[left]+ nums[right];\n            \n            avg = avg/2;\n            \n            min = Math.min(avg,min);  \n            left++;\n            right--;\n            \n            \n        }\n        return min;\n    }\n}\n```\n<br/>\n\n```python []\ndef minimum_average(nums):\n    left = 0\n    right = len(nums) - 1\n    min_avg = float(\'inf\')\n    nums.sort()\n\n    while left <= right:\n        avg = nums[left] + nums[right]\n        avg = avg / 2\n        min_avg = min(avg, min_avg)\n        left += 1\n        right -= 1\n\n    return min_avg\n\n# Example usage:\nnums = [3, 6, 9, 1, 5]\nprint(minimum_average(nums))\n\n```\n\n<br/>\n\n```C++ []\n#include <iostream>\n#include <vector>\n#include <algorithm>\n#include <limits>\n\nusing namespace std;\n\ndouble minimumAverage(vector<int>& nums) {\n    int left = 0;\n    int right = nums.size() - 1;\n    double min_avg = numeric_limits<double>::max();\n    sort(nums.begin(), nums.end());\n\n    while (left <= right) {\n        double avg = nums[left] + nums[right];\n        avg = avg / 2;\n        min_avg = min(avg, min_avg);\n        left++;\n        right--;\n    }\n\n    return min_avg;\n}\n\nint main() {\n    vector<int> nums = {3, 6, 9, 1, 5};\n    cout << minimumAverage(nums) << endl;\n    return 0;\n}\n\n```\n\n<br/>\n\n```javascript []\nfunction minimumAverage(nums) {\n    let left = 0;\n    let right = nums.length - 1;\n    let min_avg = Number.MAX_VALUE;\n    nums.sort((a, b) => a - b);\n\n    while (left <= right) {\n        let avg = nums[left] + nums[right];\n        avg = avg / 2;\n        min_avg = Math.min(avg, min_avg);\n        left++;\n        right--;\n    }\n\n    return min_avg;\n}\n\n// Example usage:\nconst nums = [3, 6, 9, 1, 5];\nconsole.log(minimumAverage(nums));\n\n```
['Two Pointers', 'C++', 'Java', 'Python3']