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 and an integer k.
An integer x is almost missing from nums if x appears in exactly one subarray of size k within nums.
Return the largest almost missing integer from nums. If no such integer exists, return -1.
Example 1:
Input: nums = [3,9,2,1,7], k = 3
Output: 7
Explanation:
[9, 2, 1] and [2, 1, 7].[3, 9, 2], [9, 2, 1], [2, 1, 7].[3, 9, 2].[2, 1, 7].[3, 9, 2], and [9, 2, 1].We return 7 since it is the largest integer that appears in exactly one subarray of size k.
Example 2:
Input: nums = [3,9,7,2,1,7], k = 4
Output: 3
Explanation:
[9, 7, 2, 1], [7, 2, 1, 7].[3, 9, 7, 2], [9, 7, 2, 1], [7, 2, 1, 7].[3, 9, 7, 2].[3, 9, 7, 2], [9, 7, 2, 1], [7, 2, 1, 7].[3, 9, 7, 2], [9, 7, 2, 1].We return 3 since it is the largest and only integer that appears in exactly one subarray of size k.
Example 3:
Input: nums = [0,0], k = 1
Output: -1
Explanation:
There is no integer that appears in only one subarray of size 1.
Constraints:
1 <= nums.length <= 500 <= nums[i] <= 501 <= k <= nums.lengthProblem summary: You are given an integer array nums and an integer k. An integer x is almost missing from nums if x appears in exactly one subarray of size k within nums. Return the largest almost missing integer from nums. If no such integer exists, return -1. A subarray is a contiguous sequence of elements within an array.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[3,9,2,1,7] 3
[3,9,7,2,1,7] 4
[0,0] 1
missing-number)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3471: Find the Largest Almost Missing Integer
class Solution {
private int[] nums;
public int largestInteger(int[] nums, int k) {
this.nums = nums;
if (k == 1) {
Map<Integer, Integer> cnt = new HashMap<>();
for (int x : nums) {
cnt.merge(x, 1, Integer::sum);
}
int ans = -1;
for (var e : cnt.entrySet()) {
if (e.getValue() == 1) {
ans = Math.max(ans, e.getKey());
}
}
return ans;
}
if (k == nums.length) {
return Arrays.stream(nums).max().getAsInt();
}
return Math.max(f(0), f(nums.length - 1));
}
private int f(int k) {
for (int i = 0; i < nums.length; ++i) {
if (i != k && nums[i] == nums[k]) {
return -1;
}
}
return nums[k];
}
}
// Accepted solution for LeetCode #3471: Find the Largest Almost Missing Integer
func largestInteger(nums []int, k int) int {
if k == 1 {
cnt := make(map[int]int)
for _, x := range nums {
cnt[x]++
}
ans := -1
for x, v := range cnt {
if v == 1 {
ans = max(ans, x)
}
}
return ans
}
n := len(nums)
if k == n {
return slices.Max(nums)
}
f := func(k int) int {
for i, x := range nums {
if i != k && x == nums[k] {
return -1
}
}
return nums[k]
}
return max(f(0), f(n-1))
}
# Accepted solution for LeetCode #3471: Find the Largest Almost Missing Integer
class Solution:
def largestInteger(self, nums: List[int], k: int) -> int:
def f(k: int) -> int:
for i, x in enumerate(nums):
if i != k and x == nums[k]:
return -1
return nums[k]
if k == 1:
cnt = Counter(nums)
return max((x for x, v in cnt.items() if v == 1), default=-1)
if k == len(nums):
return max(nums)
return max(f(0), f(len(nums) - 1))
// Accepted solution for LeetCode #3471: Find the Largest Almost Missing Integer
// 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 #3471: Find the Largest Almost Missing Integer
// class Solution {
// private int[] nums;
//
// public int largestInteger(int[] nums, int k) {
// this.nums = nums;
// if (k == 1) {
// Map<Integer, Integer> cnt = new HashMap<>();
// for (int x : nums) {
// cnt.merge(x, 1, Integer::sum);
// }
// int ans = -1;
// for (var e : cnt.entrySet()) {
// if (e.getValue() == 1) {
// ans = Math.max(ans, e.getKey());
// }
// }
// return ans;
// }
// if (k == nums.length) {
// return Arrays.stream(nums).max().getAsInt();
// }
// return Math.max(f(0), f(nums.length - 1));
// }
//
// private int f(int k) {
// for (int i = 0; i < nums.length; ++i) {
// if (i != k && nums[i] == nums[k]) {
// return -1;
// }
// }
// return nums[k];
// }
// }
// Accepted solution for LeetCode #3471: Find the Largest Almost Missing Integer
function largestInteger(nums: number[], k: number): number {
if (k === 1) {
const cnt = new Map<number, number>();
for (const x of nums) {
cnt.set(x, (cnt.get(x) || 0) + 1);
}
let ans = -1;
for (const [x, v] of cnt.entries()) {
if (v === 1 && x > ans) {
ans = x;
}
}
return ans;
}
const n = nums.length;
if (k === n) {
return Math.max(...nums);
}
const f = (k: number): number => {
for (let i = 0; i < n; i++) {
if (i !== k && nums[i] === nums[k]) {
return -1;
}
}
return nums[k];
};
return Math.max(f(0), f(n - 1));
}
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.