Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[3,5]
[-1,1,2]
[4,-1]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3678: Smallest Absent Positive Greater Than Average
class Solution {
public int smallestAbsent(int[] nums) {
Set<Integer> s = new HashSet<>();
int sum = 0;
for (int x : nums) {
s.add(x);
sum += x;
}
int ans = Math.max(1, sum / nums.length + 1);
while (s.contains(ans)) {
++ans;
}
return ans;
}
}
// Accepted solution for LeetCode #3678: Smallest Absent Positive Greater Than Average
func smallestAbsent(nums []int) int {
s := map[int]bool{}
sum := 0
for _, x := range nums {
s[x] = true
sum += x
}
ans := max(1, sum/len(nums)+1)
for s[ans] {
ans++
}
return ans
}
# Accepted solution for LeetCode #3678: Smallest Absent Positive Greater Than Average
class Solution:
def smallestAbsent(self, nums: List[int]) -> int:
s = set(nums)
ans = max(1, sum(nums) // len(nums) + 1)
while ans in s:
ans += 1
return ans
// Accepted solution for LeetCode #3678: Smallest Absent Positive Greater Than Average
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3678: Smallest Absent Positive Greater Than Average
// class Solution {
// public int smallestAbsent(int[] nums) {
// Set<Integer> s = new HashSet<>();
// int sum = 0;
// for (int x : nums) {
// s.add(x);
// sum += x;
// }
// int ans = Math.max(1, sum / nums.length + 1);
// while (s.contains(ans)) {
// ++ans;
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3678: Smallest Absent Positive Greater Than Average
function smallestAbsent(nums: number[]): number {
const s = new Set<number>(nums);
const sum = nums.reduce((a, b) => a + b, 0);
let ans = Math.max(1, Math.floor(sum / nums.length) + 1);
while (s.has(ans)) {
ans++;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.