Skip to main content
Back to problems
#3194
Easy Algorithms

Minimum average of smallest and largest elements

Array Two Pointers Sorting
85.3% acceptance
Feb 24, 2026
203
16
You have an array of floating point numbers averages which is initially empty. You are given an array nums of n integers where n is even. You repeat the following procedure n / 2 times: Remove the smallest element, minElement, and the largest element maxElement, from nums. Add (minElement + maxElement) / 2 to averages. Return the minimum element in averages.

Solution

Rust
Time O(n log n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn minimum_average(nums: Vec<i32>) -> f64 {
    let mut sorted = nums.clone();
    sorted.sort_unstable();
    let n = sorted.len();
    let mut min_avg = f64::MAX;
    for i in 0..n / 2 {
      let avg = (sorted[i] + sorted[n - 1 - i]) as f64 / 2.0;
      if avg < min_avg {
        min_avg = avg;
      }
    }
    min_avg
  }
}