[LeetCode] Smallest Absent Positive Greater Than Average

3678. Smallest Absent Positive Greater Than Average

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
int smallestAbsent(vector<int>& nums) {
long long sum = accumulate(begin(nums), end(nums), 0ll);
long long avg = sum / (long long)nums.size();
avg = max(avg, 0ll);
long long look = avg + 1;
sort(begin(nums), end(nums));
nums.erase(unique(begin(nums), end(nums)), end(nums));
for(auto& n : nums) {
if(n <= avg) continue;
if(n == look) look++;
else return look;
}
return look;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/09/19/PS/LeetCode/smallest-absent-positive-greater-than-average/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.