Problem Description
Given an integer array arr, remove the smallest 5% and largest 5% of the elements and return the mean of the remaining elements. The result is accepted if it is within 10⁻⁵ of the actual answer.
Key Insights
- Sorting the array allows for straightforward identification and removal of the smallest and largest elements.
- Since the array length is always a multiple of 20, exactly 5% of the elements from each end can be removed without rounding errors.
- The mean is computed by summing the remaining values and dividing by their count.
- The challenge mainly focuses on correct sorting and accurate slicing based on the percentage.
Space and Time Complexity
Time Complexity: O(n log n) due to the sorting step.
Space Complexity: O(n) depending on the sorting method used (or O(1) if sorting in-place).
Solution
The solution involves sorting the array so that the 5% smallest and 5% largest elements become easily accessible at the ends of the sorted array. Once sorted, calculate the number of elements to remove from each end (i.e., (5% * n)). Remove these elements by slicing the array and then compute the mean of the remaining elements by summing them and dividing by the number of elements left.