Minimum Average of Smallest and Largest Elements
IntuitionWhen I first approached this problem, I realized that to minimize the average of the smallest and largest elements, we need to pair them optimally. Sin
# Intuition
When I first approached this problem, I realized that to minimize the average of the smallest and largest elements, we need to pair them optimally. Since the average is directly influenced by the extremes in each pair, sorting the array would allow us to systematically pair the smallest elements with the largest ones to minimize their averages.

# Approach
My solution uses a two-pointer technique after sorting the array:
	1.	First, I sort the array to easily access elements in ascending order
	2.	Initialize two pointers - one at the beginning (left) and one at the end (right) of the array
	3.	For each pair of elements (the smallest unpaired element and the largest unpaired element), calculate their average
	4.	Track the minimum average found so far
	5.	Move the pointers inward (increment left, decrement right) to consider the next pair
	6.	Continue until all elements are paired (left >= right)

This approach ensures we’re always pairing the smallest remaining element with the largest remaining element, which tends to minimize the overall average.

I’ve created a detailed YouTube video explaining this approach with visualizations and code walkthrough: https://www.youtube.com/watch?v=WqvwnVQLvEM

# Complexity
- Time complexity: O(n log n)
The sorting operation dominates the time complexity at O(n log n), where n is the length of the input array. The subsequent two-pointer traversal is O(n).

- Space complexity: O(1)
We only use a constant amount of extra space regardless of input size (just a few variables for tracking).



# Code
```csharp []
public class Solution {
    public double MinimumAverage(int[] nums) {
        // Sort the list to easily access smallest and largest elements
        Array.Sort(nums);
    
        double minAverage = double.MaxValue;
        int left = 0;
        int right = nums.Count() - 1;
    
        // Process pairs until all elements are used
        while (left < right) {
            // Calculate average of current smallest and largest elements
            double currentAverage = (nums[left] + nums[right]) / 2.0;
        
            // Update minimum average if current one is smaller
            minAverage = Math.Min(minAverage, currentAverage);
        
            // Move to the next pair
            left++;
            right--;
        }
    
        return minAverage;
    }
}
```

If you found this solution helpful, please check out my YouTube channel for more detailed explanations of this and other LeetCode problems! I’d love to hear your questions and alternative approaches in the comments section. Don’t forget to subscribe for weekly algorithm and data structure content!
['C#']