Skip to main content
Back to problems
#3678
Easy Algorithms

Smallest absent positive greater than average

Array Hash Table
34.1% acceptance
Feb 25, 2026
60
5
You are given an integer array nums. Return the smallest absent positive integer in nums such that it is strictly greater than the average of all elements in nums. The average of an array is defined as the sum of all its elements divided by the number of elements.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn smallest_absent(nums: Vec<i32>) -> i32 {
    let n = nums.len() as f64;
    let avg = nums.iter().sum::<i32>() as f64 / n;
    let start = (avg.floor() as i32) + 1;
    let set: std::collections::HashSet<i32> = nums.into_iter().collect();
    let mut x = start.max(1);
    loop {
      if !set.contains(&x) { return x; }
      x += 1;
    }
  }
}