Skip to main content

Find the Largest Almost Missing Integer - Solution & Explanation

EasyArrayHash Table10 min readAsked at: Google, Bloomberg
Practice this problem

Problem Statement

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.

 

Example 1:

Input: nums = [3,9,2,1,7], k = 3

Output: 7

Explanation:

  • 1 appears in 2 subarrays of size 3: [9, 2, 1] and [2, 1, 7].
  • 2 appears in 3 subarrays of size 3: [3, 9, 2], [9, 2, 1], [2, 1, 7].
  • 3 appears in 1 subarray of size 3: [3, 9, 2].
  • 7 appears in 1 subarray of size 3: [2, 1, 7].
  • 9 appears in 2 subarrays of size 3: [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:

  • 1 appears in 2 subarrays of size 4: [9, 7, 2, 1], [7, 2, 1, 7].
  • 2 appears in 3 subarrays of size 4: [3, 9, 7, 2], [9, 7, 2, 1], [7, 2, 1, 7].
  • 3 appears in 1 subarray of size 4: [3, 9, 7, 2].
  • 7 appears in 3 subarrays of size 4: [3, 9, 7, 2], [9, 7, 2, 1], [7, 2, 1, 7].
  • 9 appears in 2 subarrays of size 4: [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 <= 50
  • 0 <= nums[i] <= 50
  • 1 <= k <= nums.length

Approach Overview

Problem Overview: You are given an integer array nums and a window size k. An integer is considered almost missing if it appears in exactly one subarray of length k. The task is to return the largest such integer. If no integer satisfies the condition, return -1.

Approach 1: Brute Force Sliding Window (O(n * k) time, O(n) space)

Generate every subarray of size k and track which numbers appear inside each window. Use a HashMap to count how many windows contain each value. For every window starting at index i, iterate through the next k elements and insert them into a temporary set so duplicates inside the same window are counted only once. Then update the global counter for those elements. After processing all windows, scan the map and return the largest number whose window count equals 1. This method is straightforward but inefficient because each window requires iterating over k elements. Time complexity is O(n * k) with O(n) extra space for counting.

Approach 2: Case Analysis with Hash Table (O(n) time, O(n) space)

The key observation is how many windows of size k can contain an element based on its index. For most positions in the array, an element belongs to multiple windows. In fact, when 1 < k < n, only the boundary elements (nums[0] and nums[n-1]) can appear in exactly one window. All interior indices are covered by at least two windows. This dramatically reduces the search space.

Handle three cases directly:

k == n: There is only one window (the entire array). Every number appears in exactly one window, so return the maximum element.

k == 1: Each window contains a single element. The number of windows containing a value equals its frequency. Use a frequency map and return the largest element with frequency 1.

1 < k < n: Only nums[0] and nums[n-1] can appear in exactly one window. Count element frequencies using a hash map from Hash Table. If either boundary value occurs exactly once in the entire array, it qualifies. Return the larger valid candidate; otherwise return -1.

This reasoning removes the need to simulate windows entirely. The algorithm becomes a single pass frequency count over the array, followed by constant-time checks. The final complexity is O(n) time with O(n) space.

Recommended for interviews: The case analysis approach. Interviewers like candidates who first reason about how many windows can contain a position instead of brute forcing every window. Mentioning the brute force sliding window shows baseline understanding, but recognizing that only boundary indices can belong to exactly one window demonstrates strong problem decomposition and knowledge of hash-based counting.

Solution

If k = 1, then each element in the array forms a subarray of size 1. In this case, we only need to find the maximum value among the elements that appear exactly once in the array.

If k = n, then the entire array forms a subarray of size n. In this case, we only need to return the maximum value in the array.

If 1 < k < n, only nums[0] and nums[n-1] can be the almost missing integers. If they appear elsewhere in the array, they are not almost missing integers. Therefore, we only need to check if nums[0] and nums[n-1] appear elsewhere in the array and return the maximum value among them.

If no almost missing integer exists, return -1.

The time complexity is O(n), and the space complexity is O(n). Here, n is the length of the array nums.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Sliding WindowO(n * k)O(n)Useful for understanding the definition of "almost missing" and verifying correctness on small inputs
Case Analysis + Hash TableO(n)O(n)Optimal approach for large arrays; relies on window coverage observations and frequency counting

Video Solution

3471. Find the Largest Almost Missing Integer | Weekly Contest 439 | LeetcodeRapid Syntax634 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Find the Largest Almost Missing Integer easy or hard?
The problem is rated Easy because the final implementation is short. The tricky part is recognizing that interior elements always belong to multiple size‑k windows, leaving only the boundary elements as candidates when 1 < k < n.
Find the Largest Almost Missing Integer Python/Java solution
Most implementations follow the same structure across languages: compute a frequency map, handle the k == 1 and k == n edge cases, then check nums[0] and nums[n-1] when 1 < k < n. The logic is simple enough to translate directly into Python dictionaries or Java HashMap implementations.
How to solve Find the Largest Almost Missing Integer in O(n)?
Count the frequency of every element using a hash map. Handle edge cases first: if k equals n, return the maximum element; if k equals 1, return the largest element with frequency 1. For 1 < k < n, only nums[0] and nums[n-1] can belong to exactly one size‑k window, so check if their frequency is 1 and return the larger valid value.
What is the best approach for Find the Largest Almost Missing Integer?
The optimal solution uses case analysis combined with a hash table for frequency counting. When 1 < k < n, only the first and last elements of the array can appear in exactly one window of size k. Count element frequencies in O(n) time and check whether nums[0] or nums[n-1] occurs exactly once. This reduces the problem to constant candidate checks after a single pass.
Is Find the Largest Almost Missing Integer asked at Google/Amazon/Meta?
This problem represents a typical array and hash table reasoning question similar to what appears in interviews at companies like Amazon, Google, and Meta. The emphasis is recognizing structural constraints of sliding windows rather than implementing complex data structures.
What data structure is used in Find the Largest Almost Missing Integer?
A hash table (hash map) is used to count the frequency of each element in the array. The frequency information helps determine whether boundary values appear exactly once, which is required for them to qualify as almost missing integers.
What is the time complexity of Find the Largest Almost Missing Integer?
The optimal algorithm runs in O(n) time with O(n) space. A single pass builds a frequency map of the array, and then constant-time checks determine whether boundary elements qualify. A naive sliding window approach would take O(n * k) time because each window requires scanning up to k elements.

Ready to solve this problem?

Practice Find the Largest Almost Missing Integer with our built-in code editor and test cases.

Practice on FleetCode