✔️ Beats 100% : Sorting : Detailed Step-by-Step Explanation - C++/Java/C#/Python/JS/Ruby/Go/Dart
Intuition\nThe problem requires us to find the minimum average of pairs formed by the smallest and largest elements in a list. My first thought is to sort the l
# Intuition\nThe problem requires us to find the minimum average of pairs formed by the smallest and largest elements in a list. My first thought is to sort the list and iteratively calculate the average of the smallest and largest elements until the list is empty.\n\n# Approach\n1. Sort the list of numbers.\n2. Initialize an empty list to store the averages.\n3. While the list is not empty:\n   - Remove the smallest and largest elements from the list.\n   - Calculate their average and add it to the list of averages.\n4. Return the minimum value from the list of averages.\n\n# Complexity\n- Time complexity: $$O(n \\log n)$$ due to the sorting step.\n- Space complexity: $$O(n)$$ to store the list of averages.\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    double minimumAverage(vector<int>& nums) {\n        sort(nums.begin(), nums.end());\n        vector<double> averages;\n        \n        while (!nums.empty()) {\n            // Remove the smallest and largest elements\n            int min_element = nums.front();\n            nums.erase(nums.begin());\n            int max_element = nums.back();\n            nums.pop_back();\n            \n            // Calculate the average of the smallest and largest elements\n            double avg = (min_element + max_element) / 2.0;\n            // Add the average to the list of averages\n            averages.push_back(avg);\n        }\n        \n        // Return the minimum average\n        return *min_element(averages.begin(), averages.end());\n    }\n};\n\n```\n```python []\nclass Solution:\n    def minimumAverage(self, nums: List[int]) -> float:\n        nums.sort()\n        averages = []\n        \n        while nums:\n            # Remove the smallest and largest elements\n            minElement = nums.pop(0)\n            maxElement = nums.pop(-1)\n            \n            # Calculate the average of the smallest and largest elements\n            avg = (minElement + maxElement) / 2\n            # Add the average to the list of averages\n            averages.append(avg)\n        \n        # Return the minimum average\n        return min(averages)\n```\n```C# []\npublic class Solution {\n    public double MinimumAverage(List<int> nums) {\n        nums.Sort();\n        List<double> averages = new List<double>();\n        \n        while (nums.Count > 0) {\n            // Remove the smallest and largest elements\n            int minElement = nums[0];\n            nums.RemoveAt(0);\n            int maxElement = nums[nums.Count - 1];\n            nums.RemoveAt(nums.Count - 1);\n            \n            // Calculate the average of the smallest and largest elements\n            double avg = (minElement + maxElement) / 2.0;\n            // Add the average to the list of averages\n            averages.Add(avg);\n        }\n        \n        // Return the minimum average\n        return averages.Min();\n    }\n}\n\n```\n```Java []\nimport java.util.*;\n\nclass Solution {\n    public double minimumAverage(List<Integer> nums) {\n        Collections.sort(nums);\n        List<Double> averages = new ArrayList<>();\n        \n        while (!nums.isEmpty()) {\n            // Remove the smallest and largest elements\n            int minElement = nums.remove(0);\n            int maxElement = nums.remove(nums.size() - 1);\n            \n            // Calculate the average of the smallest and largest elements\n            double avg = (minElement + maxElement) / 2.0;\n            // Add the average to the list of averages\n            averages.add(avg);\n        }\n        \n        // Return the minimum average\n        return Collections.min(averages);\n    }\n}\n\n```\n```javascript []\nclass Solution {\n    minimumAverage(nums) {\n        nums.sort((a, b) => a - b);\n        let averages = [];\n        \n        while (nums.length > 0) {\n            // Remove the smallest and largest elements\n            let minElement = nums.shift();\n            let maxElement = nums.pop();\n            \n            // Calculate the average of the smallest and largest elements\n            let avg = (minElement + maxElement) / 2;\n            // Add the average to the list of averages\n            averages.push(avg);\n        }\n        \n        // Return the minimum average\n        return Math.min(...averages);\n    }\n}\n\n```\n```Dart []\nclass Solution {\n  double minimumAverage(List<int> nums) {\n    nums.sort();\n    List<double> averages = [];\n    \n    while (nums.isNotEmpty) {\n      // Remove the smallest and largest elements\n      int minElement = nums.removeAt(0);\n      int maxElement = nums.removeLast();\n      \n      // Calculate the average of the smallest and largest elements\n      double avg = (minElement + maxElement) / 2.0;\n      // Add the average to the list of averages\n      averages.add(avg);\n    }\n    \n    // Return the minimum average\n    return averages.reduce((a, b) => a < b ? a : b);\n  }\n}\n\n```\n```ruby []\nclass Solution\n  def minimum_average(nums)\n    nums.sort!\n    averages = []\n    \n    while !nums.empty?\n      # Remove the smallest and largest elements\n      min_element = nums.shift\n      max_element = nums.pop\n      \n      # Calculate the average of the smallest and largest elements\n      avg = (min_element + max_element) / 2.0\n      # Add the average to the list of averages\n      averages << avg\n    end\n    \n    # Return the minimum average\n    averages.min\n  end\nend\n\n```\n```Go []\npackage main\n\nimport (\n    "fmt"\n    "math"\n    "sort"\n)\n\ntype Solution struct{}\n\nfunc (s Solution) minimumAverage(nums []int) float64 {\n    sort.Ints(nums)\n    averages := []float64{}\n    \n    for len(nums) > 0 {\n        // Remove the smallest and largest elements\n        minElement := nums[0]\n        nums = nums[1:]\n        maxElement := nums[len(nums)-1]\n        nums = nums[:len(nums)-1]\n        \n        // Calculate the average of the smallest and largest elements\n        avg := float64(minElement+maxElement) / 2.0\n        // Add the average to the list of averages\n        averages = append(averages, avg)\n    }\n    \n    // Return the minimum average\n    minAvg := math.Inf(1)\n    for _, avg := range averages {\n        if avg < minAvg {\n            minAvg = avg\n        }\n    }\n    return minAvg\n}\n\nfunc main() {\n    solution := Solution{}\n    nums := []int{1, 2, 3, 4, 5}\n    fmt.Println(solution.minimumAverage(nums)) // Output: 2.5\n}\n\n```
['Sorting', 'C++', 'Java', 'Go', 'Python3', 'JavaScript', 'C#', 'Dart']