Problem
Given an integer array arr
, return the mean of the remaining integers after removing the smallest5%
and the largest 5%
of the elements.
Answers within 10-5
of the actual answer will be considered accepted.
Examples
Example 1
|
|
Example 2
|
|
Example 3
|
|
Constraints
20 <= arr.length <= 1000
arr.length
****is a multiple of20
.0 <= arr[i] <= 10^5
Solution
Method 1 – Sorting and Slicing
Intuition
To remove the smallest and largest 5% of elements, sort the array and slice off the required number of elements from both ends. The mean of the remaining elements is the answer.
Approach
- Sort the array.
- Compute
k = n // 20
(5% of the array size). - Remove the first
k
and lastk
elements. - Calculate the mean of the remaining elements.
- Return the mean as a float.
Code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n log n)
, due to sorting the array. - 🧺 Space complexity:
O(1)
(in-place sort), orO(n)
if not in-place.