EasyArrayHash TableUpdated Sep 2026

3471. Find the Largest Almost Missing Integer

Asked at Meta, Microsoft

Difficulty

Easy

Time

O(n * k)

Space

O(n)

Pattern

Array

Companies

2

Problem

You are given an integer array nums and an integer k. A subarray is a block of k elements that sit next to each other in nums. For example, in [3,9,2,1,7] with k = 3, the subarrays are [3,9,2], [9,2,1] and [2,1,7].

A value x is almost missing if x shows up in exactly one of these size-k subarrays. Note that we count subarrays, not copies of x. If x appears twice inside the same subarray, that still counts as one subarray containing x.

Return the largest almost missing value in nums. If no value is almost missing, return -1.

Which values sit in only one window?

numswindow 13921701234Window 1 is [3,9,2] — it holds 3, 9 and 2.numswindow 23921701234Window 2 is [9,2,1] — it holds 9, 2 and 1.numswindow 33921701234Window 3 is [2,1,7] — it holds 2, 1 and 7.
With k = 3 there are three windows. The value 3 is only in the first, and 7 is only in the last, so both are almost missing.

Constraints

  • ●1 <= nums.length <= 50
  • ●0 <= nums[i] <= 50
  • ●1 <= k <= nums.length

Examples

Example 1
Inputnums = [3,9,2,1,7], k = 3
Output7
WhyThere are three subarrays of size 3. The value 3 only appears in [3,9,2], and the value 7 only appears in [2,1,7]. All other values appear in two or three subarrays. Between 3 and 7, the largest is 7.
Example 2
Inputnums = [3,9,7,2,1,7], k = 4
Output3
WhyThe size-4 subarrays are [3,9,7,2], [9,7,2,1] and [7,2,1,7]. The value 3 is in only the first one, so it is almost missing. The value 7 is in all three subarrays, so it is not. The only almost missing value is 3.
Example 3
Inputnums = [0,0], k = 1
Output-1
WhyWith k = 1 the subarrays are [0] and [0]. The value 0 appears in two subarrays, not exactly one, so nothing is almost missing and the answer is -1.

Asked At

2 companies

How to Think About It

1

Count frequencies: use a hash map to count how many times each number appears. Then filter numbers that appear at most once AND are <= target. Return the largest among them.

2

Why not just sort: sorting helps find the largest <= target, but you still need to check frequency. A hash map + single pass is simpler and equally efficient at O(n).

3

The algorithm: build frequency map. Initialize result = -1. For each number in nums, if freq[num] <= 1 and num <= target, update result = max(result, num). Return result.

4

Edge cases: all numbers appear more than once (return -1), all numbers > target (return -1), target < minimum element (return -1), empty array (return -1).

Optimal Approach

Step 1: Build a frequency map by counting occurrences of each number in nums.
Step 2: Initialize result = -1.
Step 3: For each number in nums:

  • If freq[num] <= 1 and num <= target:
  • result = max(result, num)
    Step 4: Return result.

Walkthrough for nums = [4, 1, 3, 2, 7, 4], target = 5:

  • Frequencies: {4:2, 1:1, 3:1, 2:1, 7:1}
  • num=4: freq=2, fails (2 > 1)
  • num=1: freq=1, 1<=5. result=max(-1,1)=1
  • num=3: freq=1, 3<=5. result=max(1,3)=3
  • num=2: freq=1, 2<=5. result=max(3,2)=3
  • num=7: freq=1, 7>5, fails
  • num=4: freq=2, fails
  • Result: 3

Time: O(n) for counting and filtering. Space: O(n) for the frequency map.

Count windows per value, then take the max

window count39217count12121After all three windows, 3 and 7 have count 1, while9, 2 and 1 have count 2.for each window: mark its distinct valuescount[value] += 1answer = max value with count == 1
Slide a window of size k across the array. For each window, add 1 to every distinct value inside it. The answer is the biggest value whose count is exactly 1.
ComplexityTime O(n * k)Space O(n)There are about n - k + 1 subarrays, and each one is scanned to collect its distinct values, so the work is proportional to n times k.

What Trips People Up in Real Interviews

1

Missing the "at most once" constraint. Numbers appearing exactly once OR zero times qualify. A number appearing twice or more does NOT qualify. Check freq[num] <= 1, not freq[num] == 1.

2

Forgetting to check the target constraint. The number must also be <= target. A number appearing once but greater than target doesn't qualify.

3

Returning -1 when the result variable was never updated. Initialize result to -1 so that if no number qualifies, -1 is automatically returned.

4

Using a HashSet instead of a HashMap. A HashSet tells you if a number exists but not its frequency. You need a HashMap (or Counter) to count occurrences.

5

Iterating through unique numbers instead of all numbers. If nums = [5, 5], both 5s have frequency 2. You must count all occurrences, not just unique values.

Solution Code

O(n * k) time
def largestInteger(nums, target):
    from collections import Counter
    freq = Counter(nums)
    result = -1
    for num in nums:
        if freq[num] <= 1 and num <= target:
            result = max(result, num)
    return result

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Find the Largest Almost Missing Integer problem?

Given an integer array nums and an integer target, find the largest integer that appears at most once in nums and is less than or equal to target. If no such integer exists, return -1. This tests frequency counting and filtering.

How do you solve Find the Largest Almost Missing Integer?

The optimal approach runs in O(n * k) time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Find the Largest Almost Missing Integer?

Find the Largest Almost Missing Integer is asked at Meta, Microsoft. It is a easy difficulty problem.

What are common mistakes on Find the Largest Almost Missing Integer?
  • Missing the "at most once" constraint. Numbers appearing exactly once OR zero times qualify. A number appearing twice or more does NOT qualify. Check `freq[num] <= 1`, not `freq[num] == 1`.
  • Forgetting to check the target constraint. The number must also be <= target. A number appearing once but greater than target doesn't qualify.
  • Returning -1 when the result variable was never updated. Initialize result to -1 so that if no number qualifies, -1 is automatically returned.
  • Using a `HashSet` instead of a `HashMap`. A `HashSet` tells you if a number exists but not its frequency. You need a `HashMap` (or `Counter`) to count occurrences.
  • Iterating through unique numbers instead of all numbers. If nums = [5, 5], both 5s have frequency 2. You must count all occurrences, not just unique values.