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 consisting of unique integers.
Originally, nums contained every integer within a certain range. However, some integers might have gone missing from the array.
The smallest and largest integers of the original range are still present in nums.
Return a sorted list of all the missing integers in this range. If no integers are missing, return an empty list.
Example 1:
Input: nums = [1,4,2,5]
Output: [3]
Explanation:
The smallest integer is 1 and the largest is 5, so the full range should be [1,2,3,4,5]. Among these, only 3 is missing.
Example 2:
Input: nums = [7,8,6,9]
Output: []
Explanation:
The smallest integer is 6 and the largest is 9, so the full range is [6,7,8,9]. All integers are already present, so no integer is missing.
Example 3:
Input: nums = [5,1]
Output: [2,3,4]
Explanation:
The smallest integer is 1 and the largest is 5, so the full range should be [1,2,3,4,5]. The missing integers are 2, 3, and 4.
Constraints:
2 <= nums.length <= 1001 <= nums[i] <= 100Problem summary: You are given an integer array nums consisting of unique integers. Originally, nums contained every integer within a certain range. However, some integers might have gone missing from the array. The smallest and largest integers of the original range are still present in nums. Return a sorted list of all the missing integers in this range. If no integers are missing, return an empty list.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[1,4,2,5]
[7,8,6,9]
[5,1]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3731: Find Missing Elements
class Solution {
public List<Integer> findMissingElements(int[] nums) {
int mn = 100, mx = 0;
Set<Integer> s = new HashSet<>();
for (int x : nums) {
mn = Math.min(mn, x);
mx = Math.max(mx, x);
s.add(x);
}
List<Integer> ans = new ArrayList<>();
for (int x = mn + 1; x < mx; ++x) {
if (!s.contains(x)) {
ans.add(x);
}
}
return ans;
}
}
// Accepted solution for LeetCode #3731: Find Missing Elements
func findMissingElements(nums []int) (ans []int) {
mn, mx := 100, 0
s := make(map[int]bool)
for _, x := range nums {
mn = min(mn, x)
mx = max(mx, x)
s[x] = true
}
for x := mn + 1; x < mx; x++ {
if !s[x] {
ans = append(ans, x)
}
}
return
}
# Accepted solution for LeetCode #3731: Find Missing Elements
class Solution:
def findMissingElements(self, nums: List[int]) -> List[int]:
mn, mx = min(nums), max(nums)
s = set(nums)
return [x for x in range(mn + 1, mx) if x not in s]
// Accepted solution for LeetCode #3731: Find Missing Elements
fn find_missing_elements(nums: Vec<i32>) -> Vec<i32> {
let mut nums = nums;
nums.sort_unstable();
let mut ret = vec![];
let mut prev = nums[0];
for n in nums.into_iter().skip(1) {
if n - prev != 1 {
for i in (prev + 1)..n {
ret.push(i);
}
}
prev = n;
}
ret
}
fn main() {
let ret = find_missing_elements(vec![1, 9]);
println!("ret={ret:?}");
}
#[test]
fn test() {
assert_eq!(find_missing_elements(vec![1, 4, 2, 5]), [3]);
assert_eq!(find_missing_elements(vec![7, 8, 6, 9]), []);
assert_eq!(find_missing_elements(vec![5, 1]), [2, 3, 4]);
}
// Accepted solution for LeetCode #3731: Find Missing Elements
function findMissingElements(nums: number[]): number[] {
let [mn, mx] = [100, 0];
const s = new Set<number>();
for (const x of nums) {
mn = Math.min(mn, x);
mx = Math.max(mx, x);
s.add(x);
}
const ans: number[] = [];
for (let x = mn + 1; x < mx; ++x) {
if (!s.has(x)) {
ans.push(x);
}
}
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.