Best C++ Code || Easy to understand💀💯
\n\n# Code\n\nclass Solution {\npublic:\n    double minimumAverage(vector<int>& nums) {\n        vector<double> ans;\n        sort(nums.begin(), nums.end());\n 
\n\n# Code\n```\nclass Solution {\npublic:\n    double minimumAverage(vector<int>& nums) {\n        vector<double> ans;\n        sort(nums.begin(), nums.end());\n        while(nums.size()) {\n            double f =(double) nums.front();\n            nums.erase(nums.begin());\n\n            double l = (double)nums.back();\n            nums.pop_back();\n            double avg = (l + f) / 2.0;\n            ans.push_back(avg);\n        }\n        return *min_element(ans.begin(), ans.end());\n    }\n};\n```\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n**Sorting the Vector (nums):**\n\nSorting the nums vector allows us to easily access the smallest and largest remaining elements, which are needed to compute the average for each pair efficiently.\n\n**Pairing Strategy:**\n\nAfter sorting, the function iteratively constructs pairs from the extremes of the vector (nums):\nFront (f): Represents the smallest remaining element after sorting (nums.front()).\nBack (l): Represents the largest remaining element after sorting (nums.back()).\nBy repeatedly taking the smallest (f) and largest (l) elements from the sorted vector and calculating their average, the function builds a vector ans containing all possible averages of consecutive pairs.\n\n**Calculation of Averages:**\n\nFor each pair (f, l), compute the average (f + l) / 2.0 and store it in the ans vector.\n\n**Finding the Minimum Average:**\n\nAfter populating the ans vector with all possible averages, use min_element to find the smallest value among them, which represents the minimum average of all consecutive pairs.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nSort the Vector: Start by sorting nums to facilitate easy access to the smallest and largest elements.\nIterate and Compute: Use a loop to repeatedly extract the smallest (f) and largest (l) elements from the sorted vector. Compute their average and store it in ans.\nReturn the Minimum: Finally, use min_element on the ans vector to find and return the smallest average computed.\n\n# Complexity\n- Time complexity: O(Nlogn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->
['C++']