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.
Example 1:
Input: nums = [3,5]
Output: 6
Explanation:
nums is (3 + 5) / 2 = 8 / 2 = 4.Example 2:
Input: nums = [-1,1,2]
Output: 3
Explanation:
nums is (-1 + 1 + 2) / 3 = 2 / 3 = 0.667.Example 3:
Input: nums = [4,-1]
Output: 2
Explanation:
nums is (4 + (-1)) / 2 = 3 / 2 = 1.50.
Constraints:
1 <= nums.length <= 100-100 <= nums[i] <= 100Problem Overview: You are given an integer array. Compute the array's average, then return the smallest positive integer strictly greater than that average that does not appear in the array.
Approach 1: Incremental Check (Brute Force) (Time: O(n * k), Space: O(1))
First compute the average of the array in O(n). Start checking integers from floor(average) + 1 upward. For each candidate value, scan the entire array to see if it exists. If the value is not found, return it as the answer. If it exists, increment the candidate and repeat the scan.
This approach uses only constant extra memory but performs a full array scan for every candidate value. If many consecutive integers after the average exist in the array, the repeated scans increase runtime significantly. It works for small inputs but does not scale well.
Approach 2: Hash Set Lookup (Time: O(n), Space: O(n))
Store all array values in a hash-based structure such as a HashSet or set. Building the set takes O(n) time. Compute the array average, then start from floor(average) + 1. For each candidate value, perform a constant-time hash lookup to check whether it exists in the set.
The key insight is that membership checks drop from O(n) scans to O(1) average-time lookups. As soon as you encounter a value that is not present in the set and is positive, you return it. Even if several consecutive integers appear after the average, the checks remain efficient.
This method works well for general array inputs and leverages constant-time lookups provided by a hash table. It is the standard way to handle “missing value” problems where frequent existence checks are required.
Recommended for interviews: The hash set solution. Interviewers expect you to recognize that repeated membership checks on an array are expensive and replace them with constant-time lookups using a hash table. Explaining the brute-force scan first shows problem understanding, while switching to a hash-based structure demonstrates optimization skills.
We use a hash map s to record the elements that appear in the array nums.
Then, we calculate the average value avg of the array nums, and initialize the answer ans as max(1, \lfloor avg \rfloor + 1).
If ans appears in s, we increment ans until it no longer appears in s.
Finally, we return ans.
The time complexity is O(n), and the space complexity is O(n), where n is the length of the array nums.
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Incremental Check (Brute Force) | O(n * k) | O(1) | When memory must stay minimal and input size is small |
| Hash Set Lookup | O(n) | O(n) | General case when fast membership checks are needed |
LeetCode | Solved in 2:13 mins | EP54 | 3678. Smallest Absent Positive Greater Than Average • Eduardo Nakanishi • 269 views views
Watch 9 more video solutions →Practice Smallest Absent Positive Greater Than Average with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor